forked from kyma-project/Cx1ClientGo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiam.go
174 lines (152 loc) · 5.58 KB
/
iam.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package Cx1ClientGo
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"strings"
)
func (c Cx1Client) GetAuthenticationProviders() ([]AuthenticationProvider, error) {
var idps []AuthenticationProvider
response, err := c.sendRequestIAM(http.MethodGet, "/auth/admin", "/identity-provider/instances", nil, nil)
if err != nil {
return idps, err
}
err = json.Unmarshal(response, &idps)
return idps, err
}
func (c Cx1Client) GetAuthenticationProviderByAlias(alias string) (AuthenticationProvider, error) {
var idp AuthenticationProvider
response, err := c.sendRequestIAM(http.MethodGet, "/auth/admin", fmt.Sprintf("/identity-provider/instances/%v", alias), nil, nil)
if err != nil {
return idp, err
}
err = json.Unmarshal(response, &idp)
return idp, err
}
func (c Cx1Client) CreateAuthenticationProvider(alias, providerId string) (AuthenticationProvider, error) {
idp := AuthenticationProvider{
Alias: alias,
ProviderID: providerId,
}
jsonBody, _ := json.Marshal(idp)
_, err := c.sendRequestIAM(http.MethodPost, "/auth/admin", "/identity-provider/instances", bytes.NewReader(jsonBody), nil)
if err != nil {
return AuthenticationProvider{}, err
}
return c.GetAuthenticationProviderByAlias(alias)
}
func (c Cx1Client) DeleteAuthenticationProvider(provider AuthenticationProvider) error {
_, err := c.sendRequestIAM(http.MethodDelete, "/auth/admin", fmt.Sprintf("/identity-provider/instances/%v", provider.Alias), nil, nil)
return err
}
func (p AuthenticationProvider) String() string {
return fmt.Sprintf("[%v] %v (%v)", ShortenGUID(p.ID), p.Alias, strings.ToUpper(p.ProviderID))
}
func (c Cx1Client) GetAuthenticationProviderMappers(provider AuthenticationProvider) ([]AuthenticationProviderMapper, error) {
var mappers []AuthenticationProviderMapper
response, err := c.sendRequestIAM(http.MethodGet, "/auth/admin", fmt.Sprintf("/identity-provider/instances/%v/mappers", provider.Alias), nil, nil)
if err != nil {
return mappers, err
}
err = json.Unmarshal(response, &mappers)
return mappers, err
}
func (c Cx1Client) AddAuthenticationProviderMapper(mapper AuthenticationProviderMapper) error {
jsonBody, _ := json.Marshal(mapper)
_, err := c.sendRequestIAM(http.MethodPost, "/auth/admin", fmt.Sprintf("/identity-provider/instances/%v/mappers", mapper.Alias), bytes.NewReader(jsonBody), nil)
return err
}
func (c Cx1Client) DeleteAuthenticationProviderMapper(mapper AuthenticationProviderMapper) error {
_, err := c.sendRequestIAM(http.MethodDelete, "/auth/admin", fmt.Sprintf("/identity-provider/instances/%v/mappers/%v", mapper.Alias, mapper.ID), nil, nil)
return err
}
func (m AuthenticationProviderMapper) String() string {
name := fmt.Sprintf("[%v] %v.%v mapper (%v)", ShortenGUID(m.ID), m.Alias, m.Name, m.Config.SyncMode)
switch m.Mapper {
case "saml-user-attribute-idp-mapper":
return fmt.Sprintf("%v: Set %v to IdP-provided %v by %v", name, m.Config.UserAttribute, m.Config.Name, m.Mapper)
case "saml-username-idp-mapper":
return fmt.Sprintf("%v: Set Username to IdP-provided %v by %v", name, m.Config.Template, m.Mapper)
case "custom-roles-saml-idp-mapper":
return fmt.Sprintf("%v: Add role %v when IdP-provided %v is %v by %v", name, m.Config.Role, m.Config.Name, m.Config.Value, m.Mapper)
case "custom-group-saml-idp-mapper":
return fmt.Sprintf("%v: Set group membership in IdP-provided %v by %v", name, m.Config.Name, m.Mapper)
default:
return fmt.Sprintf("%v: Type %v with config: %v", name, m.Mapper, m.Config)
}
}
// Convenience functions
func (a AuthenticationProvider) MakeDefaultMapper(attribute string) (AuthenticationProviderMapper, error) {
switch strings.ToUpper(attribute) {
case "LASTNAME":
return AuthenticationProviderMapper{
Name: "Last Name",
Alias: a.Alias,
Mapper: "saml-user-attribute-idp-mapper",
Config: AuthenticationProviderMapperConfig{
SyncMode: "FORCE",
UserAttribute: "lastName",
Format: "ATTRIBUTE_FORMAT_BASIC",
Name: "Last name",
},
}, nil
case "FIRSTNAME":
return AuthenticationProviderMapper{
Name: "First Name",
Alias: a.Alias,
Mapper: "saml-user-attribute-idp-mapper",
Config: AuthenticationProviderMapperConfig{
SyncMode: "FORCE",
UserAttribute: "firstName",
Format: "ATTRIBUTE_FORMAT_BASIC",
Name: "First name",
},
}, nil
case "EMAIL":
return AuthenticationProviderMapper{
Name: "Email",
Alias: a.Alias,
Mapper: "saml-user-attribute-idp-mapper",
Config: AuthenticationProviderMapperConfig{
SyncMode: "FORCE",
UserAttribute: "email",
Format: "ATTRIBUTE_FORMAT_BASIC",
Name: "Email",
},
}, nil
case "USERNAME":
return AuthenticationProviderMapper{
Name: "Username",
Alias: a.Alias,
Mapper: "saml-username-idp-mapper",
Config: AuthenticationProviderMapperConfig{
SyncMode: "FORCE",
Template: "${ATTRIBUTE.Username}",
Target: "LOCAL"},
}, nil
case "ROLE":
return AuthenticationProviderMapper{
Name: "Scanner Role",
Alias: a.Alias,
Mapper: "custom-roles-saml-idp-mapper",
Config: AuthenticationProviderMapperConfig{
SyncMode: "FORCE",
Value: "Scanner",
Name: "Role",
Role: "ast-app.ast-scanner",
},
}, nil
case "GROUP":
return AuthenticationProviderMapper{
Name: "Groups",
Alias: a.Alias,
Mapper: "custom-group-saml-idp-mapper",
Config: AuthenticationProviderMapperConfig{
SyncMode: "FORCE",
Name: "Groups",
},
}, nil
}
return AuthenticationProviderMapper{}, fmt.Errorf("unable to create mapper: unknown attribute. Options are: lastname, firstname, email, username, role, group")
}