This repository has been archived by the owner on Nov 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
serverinfo.go
147 lines (130 loc) · 3.93 KB
/
serverinfo.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*******************************************************************************
* Copyright (c) 2019 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package security
import (
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"strings"
)
// RegisteredTheme : A Keycloak theme
type RegisteredTheme struct {
Name string `json:"name"`
Locales []string `json:"locales"`
}
// RegisteredThemes : A collection of themes
type RegisteredThemes struct {
Common []RegisteredTheme `json:"common"`
Admin []RegisteredTheme `json:"admin"`
Login []RegisteredTheme `json:"login"`
Welcome []RegisteredTheme `json:"welcome"`
Account []RegisteredTheme `json:"account"`
Email []RegisteredTheme `json:"email"`
}
// ServerInfo : A collection of themes
type ServerInfo struct {
Themes RegisteredThemes `json:"themes"`
}
// GetServerInfo - fetch Keycloak server info
func GetServerInfo(keycloakHostname string, accesstoken string) (*ServerInfo, *SecError) {
// build REST request
url := keycloakHostname + "/auth/admin/serverinfo"
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, &SecError{errOpConnection, err, err.Error()}
}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.Header.Add("Authorization", "Bearer "+accesstoken)
req.Header.Add("Cache-Control", "no-cache")
req.Header.Add("cache-control", "no-cache")
// send request
res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, &SecError{errOpConnection, err, err.Error()}
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, &SecError{errOpResponseFormat, err, err.Error()}
}
// Handle special case http status codes
switch httpCode := res.StatusCode; {
case httpCode == http.StatusBadRequest, httpCode == http.StatusUnauthorized:
keycloakAPIError := parseKeycloakError(string(body), res.StatusCode)
kcError := errors.New(string(keycloakAPIError.ErrorDescription))
return nil, &SecError{keycloakAPIError.Error, kcError, kcError.Error()}
case httpCode != http.StatusOK:
err = errors.New(string(body))
return nil, &SecError{errOpResponse, err, err.Error()}
}
// Parse and return ServerInfo
serverInfo := ServerInfo{}
err = json.Unmarshal([]byte(body), &serverInfo)
if err != nil {
return nil, &SecError{errOpResponseFormat, err, textUnableToParse}
}
return &serverInfo, nil
}
// GetSuggestedThemes - Recommends the Codewind theme, else Che, else keycloak default
// Returns the loginTheme, accountTheme, optionalError
func GetSuggestedThemes(keycloakHostname string, accesstoken string) (string, string, *SecError) {
serverInfo, secErr := GetServerInfo(keycloakHostname, accesstoken)
if secErr != nil {
return "", "", secErr
}
loginThemes := serverInfo.Themes.Login
if len(loginThemes) == 0 {
return "", "", nil
}
themeCodewind := ""
themeChe := ""
themeKeycloak := ""
themeCodewindAccount := ""
for _, theme := range serverInfo.Themes.Login {
switch strings.ToLower(theme.Name) {
case "codewind":
{
themeCodewind = theme.Name
break
}
case "che":
{
themeChe = theme.Name
break
}
case "keycloak":
{
themeKeycloak = theme.Name
break
}
}
}
for _, theme := range serverInfo.Themes.Account {
switch strings.ToLower(theme.Name) {
case "codewind":
{
themeCodewindAccount = theme.Name
break
}
}
}
if themeCodewind != "" {
return themeCodewind, themeCodewindAccount, nil
}
if themeChe != "" {
return themeChe, themeCodewindAccount, nil
}
if themeKeycloak != "" {
return themeKeycloak, themeCodewindAccount, nil
}
return "", "", nil
}