-
Notifications
You must be signed in to change notification settings - Fork 0
/
userinfo.go
61 lines (48 loc) · 1.4 KB
/
userinfo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package oauth2
import (
"encoding/json"
"github.com/cwkr/auth-server/internal/httputil"
"github.com/cwkr/auth-server/internal/people"
"log"
"net/http"
)
type userInfoHandler struct {
peopleStore people.Store
extraClaims map[string]string
}
func (u *userInfoHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log.Printf("%s %s", r.Method, r.URL)
httputil.AllowCORS(w, r, []string{http.MethodGet, http.MethodOptions}, true)
if r.Method == http.MethodOptions {
w.WriteHeader(http.StatusNoContent)
return
}
var userID = r.Context().Value("user_id").(string)
if person, err := u.peopleStore.Lookup(userID); err == nil {
var user = User{*person, userID}
var claims = map[string]any{
ClaimSubject: userID,
}
AddProfileClaims(claims, user)
AddEmailClaims(claims, user)
AddPhoneClaims(claims, user)
AddAddressClaims(claims, user)
AddExtraClaims(claims, u.extraClaims, user, "")
var bytes, err = json.Marshal(claims)
if err != nil {
Error(w, ErrorInternal, err.Error(), http.StatusInternalServerError)
return
}
httputil.NoCache(w)
w.Header().Set("Content-Type", "application/json")
w.Write(bytes)
} else {
Error(w, ErrorInternal, err.Error(), http.StatusInternalServerError)
}
}
func UserInfoHandler(peopleStore people.Store, extraClaims map[string]string) http.Handler {
return &userInfoHandler{
peopleStore: peopleStore,
extraClaims: extraClaims,
}
}