mirror of
https://github.com/bolkedebruin/rdpgw.git
synced 2025-08-18 22:43:48 +02:00
Add tokeninfo endpoint
This commit is contained in:
parent
188f077da1
commit
22d796c5cf
3 changed files with 113 additions and 0 deletions
40
api/token.go
Normal file
40
api/token.go
Normal file
|
@ -0,0 +1,40 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/bolkedebruin/rdpgw/security"
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func (c *Config) TokenInfo(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet {
|
||||
http.Error(w, "Invalid request", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
tokens, ok := r.URL.Query()["access_token"]
|
||||
if !ok || len(tokens[0]) < 1 {
|
||||
log.Printf("Missing access_token in request")
|
||||
http.Error(w, "access_token missing in request", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
token := tokens[0]
|
||||
|
||||
info, err := security.UserInfo(context.Background(), token)
|
||||
if err != nil {
|
||||
log.Printf("Token validation failed due to %s", err)
|
||||
http.Error(w, fmt.Sprintf("token validation failed due to %s", err), http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
||||
if err = json.NewEncoder(w).Encode(info); err != nil {
|
||||
log.Printf("Cannot encode json due to %s", err)
|
||||
http.Error(w, "cannot encode json", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue