-
Notifications
You must be signed in to change notification settings - Fork 1
/
liboidcagent.go
312 lines (277 loc) · 8.2 KB
/
liboidcagent.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package liboidcagent
import (
"encoding/json"
"fmt"
"strings"
"time"
mytoken "github.com/oidc-mytoken/api/v0"
)
// TokenResponse is a parsed response from the oidc-agent
type TokenResponse struct {
// The access token
Token string
// The provider that issued the token
Issuer string
// The time when the token expires
ExpiresAt time.Time
}
// MytokenResponse is a parse response from the oidc-agent compatible with the struct from the mytoken api,
// but with ExpiresAt set instead of ExpiresIn
type MytokenResponse struct {
mytoken.MytokenResponse
OIDCIssuer string
MytokenIssuer string
// The time when the token expires
ExpiresAt time.Time
}
// AccountInfoResponse holds information about the available accounts and issuers
type AccountInfoResponse map[string]IssuerInfo
// IssuerInfo is a type for holding information about a supported issuer
type IssuerInfo struct {
// Indicates whether a public client for this issuer is available or not
HasPubClient bool `json:"pubclient"`
// Maps account short names to a bool indicating if this account is currently loaded or not
Accounts map[string]bool `json:"accounts"`
}
// TokenRequest is used to request an access token from the agent
type TokenRequest struct {
// ShortName that should be used (Can be omitted if IssuerURL is specified)
ShortName string
// IssuerURL for which an access token should be obtained (Can be omitted
// if ShortName is specified)
IssuerURL string
// MinValidPeriod specifies how long the access token should be valid at
// least. The time is given in seconds. Default is 0.
MinValidPeriod uint64
// The scopes for the requested access token
Scopes []string
// The audiences for the requested access token
Audiences []string
// A string describing the requesting application (i.e. its name). It might
// be displayed to the user, if the request must be confirmed or an account
// configuration loaded.
ApplicationHint string
}
// MytokenRequest is used to request a mytoken from the agent
type MytokenRequest struct {
// ShortName that should be used
ShortName string
// A mytoken profile describing the properties of the requested mytoken
MytokenProfile string
// A string describing the requesting application (i.e. its name). It might
// be displayed to the user, if the request must be confirmed or an account
// configuration loaded.
ApplicationHint string
}
type tokenResponse struct {
Token string `json:"access_token"`
Issuer string `json:"issuer"`
ExpiresAt int64 `json:"expires_at"`
mytoken.MytokenResponse
OIDCIssuer string `json:"oidc_issuer"`
MytokenIssuer string `json:"mytoken_issuer"`
Status string `json:"status,omitempty"`
Error string `json:"error,omitempty"`
Help string `json:"info,omitempty"`
}
type infoResponse struct {
Info json.RawMessage `json:"info"`
Status string `json:"status,omitempty"`
Error string `json:"error,omitempty"`
}
type tokenRequest struct {
Request string `json:"request"`
AccountName string `json:"account,omitempty"`
Issuer string `json:"issuer,omitempty"`
Scope string `json:"scope,omitempty"`
Audience string `json:"audience,omitempty"`
ApplicationHint string `json:"application_hint,omitempty"`
MinValidPeriod uint64 `json:"min_valid_period"`
MytokenProfile string `json:"mytoken_profile"`
}
func (c *agentConnection) parseTokenResponse(rawResponse tokenResponse) (res TokenResponse, err error) {
if rawResponse.Error != "" {
err = OIDCAgentError{
err: rawResponse.Error,
help: rawResponse.Help,
remote: c.Socket.Remote,
}
return
}
if rawResponse.Status == "failure" {
err = OIDCAgentError{
err: "unknown error",
remote: c.Socket.Remote,
}
return
}
res = TokenResponse{
Token: rawResponse.Token,
Issuer: rawResponse.Issuer,
ExpiresAt: time.Unix(rawResponse.ExpiresAt, 0),
}
return
}
func (c *agentConnection) parseMytokenResponse(rawResponse tokenResponse) (res MytokenResponse, err error) {
if rawResponse.Error != "" {
err = OIDCAgentError{
err: rawResponse.Error,
help: rawResponse.Help,
remote: c.Socket.Remote,
}
return
}
if rawResponse.Status == "failure" {
err = OIDCAgentError{
err: "unknown error",
remote: c.Socket.Remote,
}
return
}
res = MytokenResponse{
MytokenResponse: rawResponse.MytokenResponse,
OIDCIssuer: rawResponse.OIDCIssuer,
MytokenIssuer: rawResponse.MytokenIssuer,
ExpiresAt: time.Unix(rawResponse.ExpiresAt, 0),
}
return
}
// GetTokenResponse gets a TokenResponse
func GetTokenResponse(req TokenRequest) (resp TokenResponse, err error) {
if req.ShortName == "" && req.IssuerURL == "" {
err = OIDCAgentError{err: "'Shortname' and 'IssuerURL' both not provided"}
return
}
conn, err := newEncryptedConn()
if err != nil {
return
}
defer conn.close()
rawReq := tokenRequest{
Request: "access_token",
AccountName: req.ShortName,
Issuer: req.IssuerURL,
Scope: strings.Join(req.Scopes, " "),
Audience: strings.Join(req.Audiences, " "),
ApplicationHint: req.ApplicationHint,
MinValidPeriod: req.MinValidPeriod,
}
var rawResp tokenResponse
err = conn.sendJSONRequest(rawReq, &rawResp)
if err != nil {
return
}
resp, err = conn.parseTokenResponse(rawResp)
return
}
// GetAccessToken gets an access token
func GetAccessToken(req TokenRequest) (string, error) {
res, err := GetTokenResponse(req)
return res.Token, err
}
// GetMytokenResponse gets a mytoken response from the agent
func GetMytokenResponse(req MytokenRequest) (resp MytokenResponse, err error) {
if req.ShortName == "" {
err = OIDCAgentError{err: "'Shortname' not provided"}
return
}
conn, err := newEncryptedConn()
if err != nil {
return
}
defer conn.close()
rawReq := tokenRequest{
Request: "mytoken",
AccountName: req.ShortName,
MytokenProfile: req.MytokenProfile,
ApplicationHint: req.ApplicationHint,
}
var rawResp tokenResponse
err = conn.sendJSONRequest(rawReq, &rawResp)
if err != nil {
return
}
resp, err = conn.parseMytokenResponse(rawResp)
return
}
// GetMytoken gets an mytoken
func GetMytoken(req MytokenRequest) (string, error) {
res, err := GetMytokenResponse(req)
return res.Mytoken, err
}
func getLoadedAccounts() (accountNames []string, err error) {
conn, err := newEncryptedConn()
if err != nil {
return
}
defer conn.close()
req := map[string]string{"request": "loaded_accounts"}
var resp struct {
Status string `json:"status"`
Error string `json:"error,omitempty"`
Accounts []string `json:"info,omitempty"`
}
err = conn.sendJSONRequest(req, &resp)
if err != nil {
return
}
if resp.Status == "success" {
accountNames = resp.Accounts
return
}
err = fmt.Errorf("error on account request (status: %s): %s", resp.Status, resp.Error)
return
}
// GetLoadedAccounts returns a list of all accounts which are currently loaded by oidc-agent
func GetLoadedAccounts() (accountNames []string, err error) {
accountNames, err = getLoadedAccounts()
if err != nil {
err = oidcAgentErrorWrap(err)
}
return
}
// GetConfiguredAccounts returns a list of all accounts which are configured for oidc-agent
func GetConfiguredAccounts() (accounts []string, err error) {
accountInfo, err := GetAccountInfos()
if err != nil {
return nil, err
}
for _, i := range accountInfo {
for a := range i.Accounts {
accounts = append(accounts, a)
}
}
return
}
func getAccountInfos() (accountInfos AccountInfoResponse, err error) {
conn, err := newEncryptedConn()
if err != nil {
return
}
defer conn.close()
req := map[string]string{"request": "account_info"}
var resp infoResponse
err = conn.sendJSONRequest(req, &resp)
if err != nil {
return
}
if resp.Status != "success" {
err = fmt.Errorf("error on account info request (status: %s): %s", resp.Status, resp.Error)
return
}
err = json.Unmarshal(resp.Info, &accountInfos)
if err != nil {
err = fmt.Errorf("error on account info request: account info malformed: %s", err.Error())
return
}
return
}
// GetAccountInfos returns information about all issuers and their available account names and if those are loaded or
// not
func GetAccountInfos() (info AccountInfoResponse, err error) {
info, err = getAccountInfos()
if err != nil {
err = oidcAgentErrorWrap(err)
}
return
}