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 pathkeycloak.go
95 lines (84 loc) · 2.45 KB
/
keycloak.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
package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
)
type KeycloakUsersArray []KeycloakSingleUserJson
type KeycloakSingleUserJson struct {
ID string `json:"id"`
Username string `json:"username"`
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
Email string `json:"email"`
Enabled bool `json:"enabled"`
Attributes map[string][]string `json:"attributes"`
}
func decodeKeycloakUsersArray(body io.ReadCloser) (KeycloakUsersArray, error) {
decoder := json.NewDecoder(body)
var kc KeycloakUsersArray
if decoder.Decode(&kc) != nil {
return nil, errors.New("Failed to parse json")
}
if len(kc) == 0 {
return nil, errors.New("Empty user array")
}
return kc, nil
}
func getKeycloakToken() (string, error) {
return loginKeycloak(userhelper, passhelper)
}
func loginKeycloak(username string, password string) (string, error) {
token, err := config.PasswordCredentialsToken(context.Background(), username, password)
if err != nil {
fmt.Println(err)
return "", errors.New("keycloak login failed")
}
return token.AccessToken, nil
}
func getRequest(url string, accessToken string) (io.ReadCloser, error) {
client := &http.Client{}
request, _ := http.NewRequest("GET", url, nil)
request.Header.Set("Authorization", "Bearer "+accessToken)
response, err := client.Do(request)
if err != nil {
return nil, errors.New("The HTTP request failed with error")
}
return response.Body, nil
}
func getUserArray(token string, username string) (KeycloakSingleUserJson, error) {
url := usersEndpoint + "?username=" + username
body, err := getRequest(url, token)
if err != nil {
return KeycloakSingleUserJson{}, err
}
arr, err := decodeKeycloakUsersArray(body)
defer body.Close()
if err != nil {
return KeycloakSingleUserJson{}, err
}
if arr[0].Enabled == false {
return KeycloakSingleUserJson{}, errors.New("Account is disabled")
}
return arr[0], nil
}
func getUsersArray(token string) (KeycloakUsersArray, error) {
body, err := getRequest(usersEndpoint, token)
if err != nil {
return nil, err
}
arr, err := decodeKeycloakUsersArray(body)
defer body.Close()
if err != nil {
return nil, err
}
return arr, nil
}
func prepareResponse(w http.ResponseWriter, body interface{}) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(body)
}