-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoken_exchange.go
More file actions
268 lines (218 loc) · 7.97 KB
/
Copy pathtoken_exchange.go
File metadata and controls
268 lines (218 loc) · 7.97 KB
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
package idjag
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"time"
)
// TokenExchangeClient handles OAuth 2.0 token exchange requests per RFC 8693.
type TokenExchangeClient struct {
// TokenURL is the authorization server's token endpoint.
TokenURL string
// HTTPClient is the HTTP client to use for requests.
// If nil, http.DefaultClient is used.
HTTPClient *http.Client
// ClientID is the OAuth client identifier.
// If set, it will be included in the request.
ClientID string
// ClientSecret is the OAuth client secret.
// If set, HTTP Basic authentication will be used.
ClientSecret string
}
// NewTokenExchangeClient creates a new token exchange client.
func NewTokenExchangeClient(tokenURL string) *TokenExchangeClient {
return &TokenExchangeClient{
TokenURL: tokenURL,
HTTPClient: &http.Client{Timeout: 30 * time.Second},
}
}
// WithCredentials sets client credentials for authentication.
func (c *TokenExchangeClient) WithCredentials(clientID, clientSecret string) *TokenExchangeClient {
c.ClientID = clientID
c.ClientSecret = clientSecret
return c
}
// WithHTTPClient sets a custom HTTP client.
func (c *TokenExchangeClient) WithHTTPClient(client *http.Client) *TokenExchangeClient {
c.HTTPClient = client
return c
}
// TokenExchangeRequest represents an RFC 8693 token exchange request.
type TokenExchangeRequest struct {
// SubjectToken is the security token being exchanged (required).
// For ID-JAG, this is the signed assertion JWT.
SubjectToken string
// SubjectTokenType identifies the type of subject token (required).
// For ID-JAG assertions, use TokenTypeJWT.
SubjectTokenType string
// ActorToken is an optional security token representing the actor.
ActorToken string
// ActorTokenType identifies the type of actor token (required if ActorToken is set).
ActorTokenType string
// RequestedTokenType identifies the type of token being requested.
// Defaults to TokenTypeAccessToken if not specified.
RequestedTokenType string
// Scope is the requested scope for the issued token.
Scope string
// Resource is the target resource for the issued token.
Resource string
// Audience is the target audience for the issued token.
Audience string
}
// TokenExchangeResponse represents an RFC 8693 token exchange response.
type TokenExchangeResponse struct {
// AccessToken is the issued security token.
AccessToken string `json:"access_token"`
// IssuedTokenType identifies the type of token issued.
IssuedTokenType string `json:"issued_token_type"`
// TokenType is typically "Bearer".
TokenType string `json:"token_type"`
// ExpiresIn is the lifetime in seconds of the access token.
ExpiresIn int `json:"expires_in,omitempty"`
// Scope is the scope of the access token.
Scope string `json:"scope,omitempty"`
// RefreshToken is an optional refresh token.
RefreshToken string `json:"refresh_token,omitempty"`
}
// Exchange performs a token exchange request.
func (c *TokenExchangeClient) Exchange(ctx context.Context, req *TokenExchangeRequest) (*TokenExchangeResponse, error) {
if req.SubjectToken == "" {
return nil, fmt.Errorf("%w: subject_token required", ErrTokenExchangeFailed)
}
if req.SubjectTokenType == "" {
return nil, fmt.Errorf("%w: subject_token_type required", ErrTokenExchangeFailed)
}
// Build form data
data := url.Values{}
data.Set("grant_type", GrantTypeTokenExchange)
data.Set("subject_token", req.SubjectToken)
data.Set("subject_token_type", req.SubjectTokenType)
if req.ActorToken != "" {
data.Set("actor_token", req.ActorToken)
if req.ActorTokenType == "" {
return nil, fmt.Errorf("%w: actor_token_type required when actor_token is set", ErrTokenExchangeFailed)
}
data.Set("actor_token_type", req.ActorTokenType)
}
if req.RequestedTokenType != "" {
data.Set("requested_token_type", req.RequestedTokenType)
}
if req.Scope != "" {
data.Set("scope", req.Scope)
}
if req.Resource != "" {
data.Set("resource", req.Resource)
}
if req.Audience != "" {
data.Set("audience", req.Audience)
}
// Add client_id if set and not using Basic auth
if c.ClientID != "" && c.ClientSecret == "" {
data.Set("client_id", c.ClientID)
}
httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, c.TokenURL, strings.NewReader(data.Encode()))
if err != nil {
return nil, fmt.Errorf("%w: %v", ErrTokenExchangeFailed, err)
}
httpReq.Header.Set(HeaderContentType, ContentTypeFormURLEncoded)
// Add Basic auth if credentials are set
if c.ClientID != "" && c.ClientSecret != "" {
httpReq.SetBasicAuth(c.ClientID, c.ClientSecret)
}
client := c.HTTPClient
if client == nil {
client = http.DefaultClient
}
resp, err := client.Do(httpReq)
if err != nil {
return nil, fmt.Errorf("%w: %v", ErrTokenExchangeFailed, err)
}
defer func() { _ = resp.Body.Close() }()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("%w: failed to read response: %v", ErrTokenExchangeFailed, err)
}
if resp.StatusCode != http.StatusOK {
var errResp TokenErrorResponse
if err := json.Unmarshal(body, &errResp); err == nil && errResp.Error != "" {
return nil, fmt.Errorf("%w: %s - %s", ErrTokenExchangeFailed, errResp.Error, errResp.ErrorDescription)
}
return nil, fmt.Errorf("%w: status %d", ErrTokenExchangeFailed, resp.StatusCode)
}
var tokenResp TokenExchangeResponse
if err := json.Unmarshal(body, &tokenResp); err != nil {
return nil, fmt.Errorf("%w: failed to parse response: %v", ErrTokenExchangeFailed, err)
}
return &tokenResp, nil
}
// ExchangeAssertion is a convenience method for exchanging an ID-JAG assertion.
// It sets the subject token type to JWT automatically.
func (c *TokenExchangeClient) ExchangeAssertion(ctx context.Context, assertion string, scope string) (*TokenExchangeResponse, error) {
return c.Exchange(ctx, &TokenExchangeRequest{
SubjectToken: assertion,
SubjectTokenType: TokenTypeJWT,
Scope: scope,
})
}
// JWTBearerClient handles JWT bearer assertion grants per RFC 7523.
type JWTBearerClient struct {
// TokenURL is the authorization server's token endpoint.
TokenURL string
// HTTPClient is the HTTP client to use for requests.
HTTPClient *http.Client
// ClientID is the OAuth client identifier.
ClientID string
}
// NewJWTBearerClient creates a new JWT bearer client.
func NewJWTBearerClient(tokenURL string) *JWTBearerClient {
return &JWTBearerClient{
TokenURL: tokenURL,
HTTPClient: &http.Client{Timeout: 30 * time.Second},
}
}
// Exchange exchanges a JWT assertion for an access token using JWT bearer grant.
func (c *JWTBearerClient) Exchange(ctx context.Context, assertion, scope string) (*TokenExchangeResponse, error) {
data := url.Values{}
data.Set("grant_type", GrantTypeJWTBearer)
data.Set("assertion", assertion)
if scope != "" {
data.Set("scope", scope)
}
if c.ClientID != "" {
data.Set("client_id", c.ClientID)
}
httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, c.TokenURL, strings.NewReader(data.Encode()))
if err != nil {
return nil, fmt.Errorf("%w: %v", ErrTokenExchangeFailed, err)
}
httpReq.Header.Set(HeaderContentType, ContentTypeFormURLEncoded)
client := c.HTTPClient
if client == nil {
client = http.DefaultClient
}
resp, err := client.Do(httpReq)
if err != nil {
return nil, fmt.Errorf("%w: %v", ErrTokenExchangeFailed, err)
}
defer func() { _ = resp.Body.Close() }()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("%w: failed to read response: %v", ErrTokenExchangeFailed, err)
}
if resp.StatusCode != http.StatusOK {
var errResp TokenErrorResponse
if err := json.Unmarshal(body, &errResp); err == nil && errResp.Error != "" {
return nil, fmt.Errorf("%w: %s - %s", ErrTokenExchangeFailed, errResp.Error, errResp.ErrorDescription)
}
return nil, fmt.Errorf("%w: status %d", ErrTokenExchangeFailed, resp.StatusCode)
}
var tokenResp TokenExchangeResponse
if err := json.Unmarshal(body, &tokenResp); err != nil {
return nil, fmt.Errorf("%w: failed to parse response: %v", ErrTokenExchangeFailed, err)
}
return &tokenResp, nil
}