This repository has been archived by the owner on Jun 24, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprofile.go
127 lines (115 loc) · 2.7 KB
/
profile.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package main
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"strings"
)
type ProfileRequest struct {
Mxid string `json:"mxid"`
Localpart string `json:"localpart"`
Domain string `json:"domain"`
}
type ProfileResponse struct {
Profile ProfileBody `json:"profile"`
}
type Profile3PIDS struct {
Medium string `json:"medium,omitempty"`
Address string `json:"address,omitempty"`
}
type ProfileBody struct {
DisplayName string `json:"display_name,omitempty"`
AvatarURL string `json:"avatar_url,omitempty"`
ThreePIDS []Profile3PIDS `json:"threepids,omitempty"`
Roles []string `json:"roles,omitempty"`
}
func getDisplayName(user KeycloakSingleUserJson) string {
name := strings.TrimSpace(user.FirstName + " " + user.LastName)
if name == "" {
name = user.Username
}
return name
}
func getAvatarURL(attributes map[string][]string) string {
if _, value := attributes["avatar_url"]; value {
return attributes["avatar_url"][0]
}
return ""
}
func getProfile3PIDS(user KeycloakSingleUserJson) []Profile3PIDS {
var tpids []Profile3PIDS
if user.Email != "" {
tpids = append(tpids, Profile3PIDS{
Medium: "email",
Address: user.Email,
})
}
for key, value := range user.Attributes {
if strings.Contains(valid3pids, key) {
tpids = append(tpids, Profile3PIDS{
Medium: key,
Address: value[0],
})
}
}
return tpids
}
// Keycloak groups are used as roles
func getProfileRoles(token string, id string) ([]string, error) {
url := usersEndpoint + "/" + id + "/groups"
body, err := getRequest(url, token)
if err != nil {
return nil, err
}
var kc []struct {
Name string `json:"name"`
}
if json.NewDecoder(body).Decode(&kc) != nil {
return nil, errors.New("Failed to parse json")
}
defer body.Close()
roles := make([]string, len(kc))
for i, v := range kc {
roles[i] = v.Name
}
return roles, nil
}
func buildProfile(username string) (*ProfileBody, error) {
token, err := getKeycloakToken()
if err != nil {
return nil, err
}
user, err := getUserArray(token, username)
if err != nil {
return nil, err
}
roles, err := getProfileRoles(token, user.ID)
if err != nil {
return nil, err
}
return &ProfileBody{
DisplayName: getDisplayName(user),
AvatarURL: getAvatarURL(user.Attributes),
ThreePIDS: getProfile3PIDS(user),
Roles: roles,
}, nil
}
func Profile(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
return
}
var req ProfileRequest
if json.NewDecoder(r.Body).Decode(&req) != nil {
fmt.Println("Invalid json request")
return
}
var res ProfileResponse
profile, err := buildProfile(req.Localpart)
if err != nil {
fmt.Println(err)
} else {
res.Profile = *profile
}
prepareResponse(w, res)
}