| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package oauthex |
| 9 |
|
| 10 |
import ( |
| 11 |
"context" |
| 12 |
"fmt" |
| 13 |
"net/http" |
| 14 |
"strings" |
| 15 |
|
| 16 |
"golang.org/x/oauth2" |
| 17 |
) |
| 18 |
|
| 19 |
|
| 20 |
const ( |
| 21 |
|
| 22 |
TokenTypeIDToken = "urn:ietf:params:oauth:token-type:id_token" |
| 23 |
|
| 24 |
|
| 25 |
TokenTypeSAML2 = "urn:ietf:params:oauth:token-type:saml2" |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
TokenTypeIDJAG = "urn:ietf:params:oauth:token-type:id-jag" |
| 30 |
|
| 31 |
|
| 32 |
GrantTypeTokenExchange = "urn:ietf:params:oauth:grant-type:token-exchange" |
| 33 |
) |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
type TokenExchangeRequest struct { |
| 40 |
|
| 41 |
|
| 42 |
RequestedTokenType string |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
Audience string |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
Resource string |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
Scope []string |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
SubjectToken string |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
SubjectTokenType string |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
func ExchangeToken( |
| 82 |
ctx context.Context, |
| 83 |
tokenEndpoint string, |
| 84 |
req *TokenExchangeRequest, |
| 85 |
clientCreds *ClientCredentials, |
| 86 |
httpClient *http.Client, |
| 87 |
) (*oauth2.Token, error) { |
| 88 |
if tokenEndpoint == "" { |
| 89 |
return nil, fmt.Errorf("token endpoint is required") |
| 90 |
} |
| 91 |
if req == nil { |
| 92 |
return nil, fmt.Errorf("token exchange request is required") |
| 93 |
} |
| 94 |
if clientCreds == nil { |
| 95 |
return nil, fmt.Errorf("client credentials are required") |
| 96 |
} |
| 97 |
if err := clientCreds.Validate(); err != nil { |
| 98 |
return nil, fmt.Errorf("invalid client credentials: %w", err) |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
if req.RequestedTokenType == "" { |
| 103 |
return nil, fmt.Errorf("requested_token_type is required") |
| 104 |
} |
| 105 |
if req.Audience == "" { |
| 106 |
return nil, fmt.Errorf("audience is required") |
| 107 |
} |
| 108 |
if req.Resource == "" { |
| 109 |
return nil, fmt.Errorf("resource is required") |
| 110 |
} |
| 111 |
if req.SubjectToken == "" { |
| 112 |
return nil, fmt.Errorf("subject_token is required") |
| 113 |
} |
| 114 |
if req.SubjectTokenType == "" { |
| 115 |
return nil, fmt.Errorf("subject_token_type is required") |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
if err := checkURLScheme(tokenEndpoint); err != nil { |
| 120 |
return nil, fmt.Errorf("invalid token endpoint: %w", err) |
| 121 |
} |
| 122 |
if err := checkURLScheme(req.Audience); err != nil { |
| 123 |
return nil, fmt.Errorf("invalid audience: %w", err) |
| 124 |
} |
| 125 |
if err := checkURLScheme(req.Resource); err != nil { |
| 126 |
return nil, fmt.Errorf("invalid resource: %w", err) |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
cfg := &oauth2.Config{ |
| 134 |
ClientID: clientCreds.ClientID, |
| 135 |
Endpoint: oauth2.Endpoint{ |
| 136 |
TokenURL: tokenEndpoint, |
| 137 |
AuthStyle: oauth2.AuthStyleInParams, |
| 138 |
}, |
| 139 |
} |
| 140 |
|
| 141 |
if clientCreds.ClientSecretAuth != nil { |
| 142 |
cfg.ClientSecret = clientCreds.ClientSecretAuth.ClientSecret |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
if httpClient == nil { |
| 147 |
httpClient = http.DefaultClient |
| 148 |
} |
| 149 |
ctxWithClient := context.WithValue(ctx, oauth2.HTTPClient, httpClient) |
| 150 |
|
| 151 |
|
| 152 |
opts := []oauth2.AuthCodeOption{ |
| 153 |
oauth2.SetAuthURLParam("grant_type", GrantTypeTokenExchange), |
| 154 |
oauth2.SetAuthURLParam("requested_token_type", req.RequestedTokenType), |
| 155 |
oauth2.SetAuthURLParam("audience", req.Audience), |
| 156 |
oauth2.SetAuthURLParam("resource", req.Resource), |
| 157 |
oauth2.SetAuthURLParam("subject_token", req.SubjectToken), |
| 158 |
oauth2.SetAuthURLParam("subject_token_type", req.SubjectTokenType), |
| 159 |
} |
| 160 |
if len(req.Scope) > 0 { |
| 161 |
opts = append(opts, oauth2.SetAuthURLParam("scope", strings.Join(req.Scope, " "))) |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
token, err := cfg.Exchange( |
| 167 |
ctxWithClient, |
| 168 |
"", |
| 169 |
opts..., |
| 170 |
) |
| 171 |
if err != nil { |
| 172 |
return nil, fmt.Errorf("token exchange request failed: %w", err) |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
issuedTokenType, _ := token.Extra("issued_token_type").(string) |
| 178 |
if issuedTokenType == "" { |
| 179 |
return nil, fmt.Errorf("response missing required field: issued_token_type") |
| 180 |
} |
| 181 |
|
| 182 |
return token, nil |
| 183 |
} |