forked from jarias/stormpath-sdk-go
/
me.go
84 lines (74 loc) · 1.83 KB
/
me.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package stormpathweb
import (
"net/http"
"github.com/jarias/stormpath-sdk-go"
"golang.org/x/net/context"
)
type meHandler struct {
}
func (h meHandler) ServeHTTP(w http.ResponseWriter, r *http.Request, ctx context.Context) {
if r.Method == http.MethodGet {
if ctx, ok := isAuthenticated(w, r, ctx); ok {
w.Header().Set("Cache-Control", "no-store, no-cache")
w.Header().Set("Pragma", "no-cache")
account := expandAccountAttributes(ctx.Value(AccountKey).(*stormpath.Account))
respondJSON(w, accountModel(account), http.StatusOK)
return
}
unauthorizedRequest(w, r, ctx)
return
}
methodNotAllowed(w, r, ctx)
}
func expandAccountAttributes(account *stormpath.Account) *stormpath.Account {
criteria := stormpath.MakeAccountCriteria()
for attribute, shouldExpand := range Config.MeExpand {
switch attribute {
case "apiKeys":
if shouldExpand.(bool) {
criteria = criteria.WithAPIKeys()
}
break
case "applications":
if shouldExpand.(bool) {
criteria = criteria.WithApplications()
}
break
case "customData":
if shouldExpand.(bool) {
criteria = criteria.WithCustomData()
}
break
case "directory":
if shouldExpand.(bool) {
criteria = criteria.WithDirectory()
}
break
case "groupMemberships":
if shouldExpand.(bool) {
criteria = criteria.WithGroupMemberships(stormpath.DefaultPageRequest)
}
break
case "groups":
if shouldExpand.(bool) {
criteria = criteria.WithGroups(stormpath.DefaultPageRequest)
}
break
case "providerData":
if shouldExpand.(bool) {
criteria = criteria.WithProviderData()
}
break
case "tenant":
if shouldExpand.(bool) {
criteria = criteria.WithTenant()
}
break
}
}
expandedAccount, err := stormpath.GetAccount(account.Href, criteria)
if err != nil {
return account
}
return expandedAccount
}