Debian Codegraph @

pkg/oidc/token_request.go

raw
1 package oidc
2
3 import (
4 "encoding/json"
5 "fmt"
6 "net/http"
7 "net/url"
8 "slices"
9 "time"
10
11 jose "github.com/go-jose/go-jose/v4"
12 )
13
14 const (
15 // GrantTypeCode defines the grant_type `authorization_code` used for the Token Request in the Authorization Code Flow
16 GrantTypeCode GrantType = "authorization_code"
17
18 // GrantTypeRefreshToken defines the grant_type `refresh_token` used for the Token Request in the Refresh Token Flow
19 GrantTypeRefreshToken GrantType = "refresh_token"
20
21 // GrantTypeClientCredentials defines the grant_type `client_credentials` used for the Token Request in the Client Credentials Token Flow
22 GrantTypeClientCredentials GrantType = "client_credentials"
23
24 // GrantTypeBearer defines the grant_type `urn:ietf:params:oauth:grant-type:jwt-bearer` used for the JWT Authorization Grant
25 GrantTypeBearer GrantType = "urn:ietf:params:oauth:grant-type:jwt-bearer"
26
27 // GrantTypeTokenExchange defines the grant_type `urn:ietf:params:oauth:grant-type:token-exchange` used for the OAuth Token Exchange Grant
28 GrantTypeTokenExchange GrantType = "urn:ietf:params:oauth:grant-type:token-exchange"
29
30 // GrantTypeImplicit defines the grant type `implicit` used for implicit flows that skip the generation and exchange of an Authorization Code
31 GrantTypeImplicit GrantType = "implicit"
32
33 // GrantTypeDeviceCode
34 GrantTypeDeviceCode GrantType = "urn:ietf:params:oauth:grant-type:device_code"
35
36 // ClientAssertionTypeJWTAssertion defines the client_assertion_type `urn:ietf:params:oauth:client-assertion-type:jwt-bearer`
37 // used for the OAuth JWT Profile Client Authentication
38 ClientAssertionTypeJWTAssertion = "urn:ietf:params:oauth:client-assertion-type:jwt-bearer"
39 )
40
41 var AllGrantTypes = []GrantType{
42 GrantTypeCode, GrantTypeRefreshToken, GrantTypeClientCredentials,
43 GrantTypeBearer, GrantTypeTokenExchange, GrantTypeImplicit,
44 GrantTypeDeviceCode, ClientAssertionTypeJWTAssertion,
45 }
46
47 type GrantType string
48
49 const (
50 AccessTokenType TokenType = "urn:ietf:params:oauth:token-type:access_token"
51 RefreshTokenType TokenType = "urn:ietf:params:oauth:token-type:refresh_token"
52 IDTokenType TokenType = "urn:ietf:params:oauth:token-type:id_token"
53 JWTTokenType TokenType = "urn:ietf:params:oauth:token-type:jwt"
54 )
55
56 var AllTokenTypes = []TokenType{
57 AccessTokenType, RefreshTokenType, IDTokenType, JWTTokenType,
58 }
59
60 type TokenType string
61
62 func (t TokenType) IsSupported() bool {
63 return slices.Contains(AllTokenTypes, t)
64 }
65
66 type TokenRequest interface {
67 // GrantType GrantType `schema:"grant_type"`
68 GrantType() GrantType
69 }
70
71 type TokenRequestType GrantType
72
73 type AccessTokenRequest struct {
74 Code string `schema:"code"`
75 RedirectURI string `schema:"redirect_uri"`
76 ClientID string `schema:"client_id"`
77 ClientSecret string `schema:"client_secret,omitempty"`
78 CodeVerifier string `schema:"code_verifier,omitempty"`
79 ClientAssertion string `schema:"client_assertion,omitempty"`
80 ClientAssertionType string `schema:"client_assertion_type,omitempty"`
81 }
82
83 func (a *AccessTokenRequest) GrantType() GrantType {
84 return GrantTypeCode
85 }
86
87 // SetClientID implements op.AuthenticatedTokenRequest
88 func (a *AccessTokenRequest) SetClientID(clientID string) {
89 a.ClientID = clientID
90 }
91
92 // SetClientSecret implements op.AuthenticatedTokenRequest
93 func (a *AccessTokenRequest) SetClientSecret(clientSecret string) {
94 a.ClientSecret = clientSecret
95 }
96
97 // IsSetClientAssertion implements op.AuthenticatedTokenRequestAuthMethodChecker
98 func (a *AccessTokenRequest) IsSetClientAssertion() bool {
99 return a.ClientAssertion != "" && a.ClientAssertionType != ""
100 }
101
102 // IsSetClientIDAndClientSecret implements op.AuthenticatedTokenRequestAuthMethodChecker
103 func (a *AccessTokenRequest) IsSetClientIDAndClientSecret() bool {
104 return a.ClientID != "" && a.ClientSecret != ""
105 }
106
107 // RefreshTokenRequest is not useful for making refresh requests because the
108 // grant_type is not included explicitly but rather implied.
109 type RefreshTokenRequest struct {
110 RefreshToken string `schema:"refresh_token"`
111 Scopes SpaceDelimitedArray `schema:"scope"`
112 ClientID string `schema:"client_id"`
113 ClientSecret string `schema:"client_secret"`
114 ClientAssertion string `schema:"client_assertion"`
115 ClientAssertionType string `schema:"client_assertion_type"`
116 }
117
118 func (a *RefreshTokenRequest) GrantType() GrantType {
119 return GrantTypeRefreshToken
120 }
121
122 // SetClientID implements op.AuthenticatedTokenRequest
123 func (a *RefreshTokenRequest) SetClientID(clientID string) {
124 a.ClientID = clientID
125 }
126
127 // SetClientSecret implements op.AuthenticatedTokenRequest
128 func (a *RefreshTokenRequest) SetClientSecret(clientSecret string) {
129 a.ClientSecret = clientSecret
130 }
131
132 // IsSetClientAssertion implements op.AuthenticatedTokenRequestAuthMethodChecker
133 func (a *RefreshTokenRequest) IsSetClientAssertion() bool {
134 return a.ClientAssertion != "" && a.ClientAssertionType != ""
135 }
136
137 // IsSetClientIDAndClientSecret implements op.AuthenticatedTokenRequestAuthMethodChecker
138 func (a *RefreshTokenRequest) IsSetClientIDAndClientSecret() bool {
139 return a.ClientID != "" && a.ClientSecret != ""
140 }
141
142 type JWTTokenRequest struct {
143 Issuer string `json:"iss"`
144 Subject string `json:"sub"`
145 Scopes SpaceDelimitedArray `json:"-"`
146 Audience Audience `json:"aud"`
147 IssuedAt Time `json:"iat"`
148 ExpiresAt Time `json:"exp"`
149
150 private map[string]any
151 }
152
153 func (j *JWTTokenRequest) MarshalJSON() ([]byte, error) {
154 type Alias JWTTokenRequest
155 a := (*Alias)(j)
156
157 b, err := json.Marshal(a)
158 if err != nil {
159 return nil, err
160 }
161
162 if len(j.private) == 0 {
163 return b, nil
164 }
165
166 err = json.Unmarshal(b, &j.private)
167 if err != nil {
168 return nil, fmt.Errorf("jws: invalid map of custom claims %v", j.private)
169 }
170
171 return json.Marshal(j.private)
172 }
173
174 func (j *JWTTokenRequest) UnmarshalJSON(data []byte) error {
175 type Alias JWTTokenRequest
176 a := (*Alias)(j)
177
178 err := json.Unmarshal(data, a)
179 if err != nil {
180 return err
181 }
182
183 err = json.Unmarshal(data, &j.private)
184 if err != nil {
185 return err
186 }
187
188 return nil
189 }
190
191 func (j *JWTTokenRequest) GetCustomClaim(key string) any {
192 return j.private[key]
193 }
194
195 // GetIssuer implements the Claims interface
196 func (j *JWTTokenRequest) GetIssuer() string {
197 return j.Issuer
198 }
199
200 // GetAudience implements the Claims and TokenRequest interfaces
201 func (j *JWTTokenRequest) GetAudience() []string {
202 return j.Audience
203 }
204
205 // GetExpiration implements the Claims interface
206 func (j *JWTTokenRequest) GetExpiration() time.Time {
207 return j.ExpiresAt.AsTime()
208 }
209
210 // GetIssuedAt implements the Claims interface
211 func (j *JWTTokenRequest) GetIssuedAt() time.Time {
212 return j.IssuedAt.AsTime()
213 }
214
215 // GetNonce implements the Claims interface
216 func (j *JWTTokenRequest) GetNonce() string {
217 return ""
218 }
219
220 // GetAuthenticationContextClassReference implements the Claims interface
221 func (j *JWTTokenRequest) GetAuthenticationContextClassReference() string {
222 return ""
223 }
224
225 // GetAuthTime implements the Claims interface
226 func (j *JWTTokenRequest) GetAuthTime() time.Time {
227 return time.Time{}
228 }
229
230 // GetAuthorizedParty implements the Claims interface
231 func (j *JWTTokenRequest) GetAuthorizedParty() string {
232 return ""
233 }
234
235 // SetSignatureAlgorithm implements the Claims interface
236 func (j *JWTTokenRequest) SetSignatureAlgorithm(_ jose.SignatureAlgorithm) {}
237
238 // GetSubject implements the TokenRequest interface
239 func (j *JWTTokenRequest) GetSubject() string {
240 return j.Subject
241 }
242
243 // GetScopes implements the TokenRequest interface
244 func (j *JWTTokenRequest) GetScopes() []string {
245 return j.Scopes
246 }
247
248 type TokenExchangeRequest struct {
249 GrantType GrantType `schema:"grant_type"`
250 SubjectToken string `schema:"subject_token"`
251 SubjectTokenType TokenType `schema:"subject_token_type"`
252 ActorToken string `schema:"actor_token"`
253 ActorTokenType TokenType `schema:"actor_token_type"`
254 Resource []string `schema:"resource"`
255 Audience Audience `schema:"audience"`
256 Scopes SpaceDelimitedArray `schema:"scope"`
257 RequestedTokenType TokenType `schema:"requested_token_type"`
258 }
259
260 type ClientCredentialsRequest struct {
261 GrantType GrantType `schema:"grant_type,omitempty"`
262 Scope SpaceDelimitedArray `schema:"scope"`
263 ClientID string `schema:"client_id"`
264 ClientSecret string `schema:"client_secret"`
265 ClientAssertion string `schema:"client_assertion,omitempty"`
266 ClientAssertionType string `schema:"client_assertion_type,omitempty"`
267 }
268
269 // Deprecated: This function is no longer invoked because it violates
270 // RFC 6749 ยง2.3: The client MUST NOT use more than one authentication method in each request.
271 //
272 // Configure the OAuth2 endpoint AuthStyle (e.g., oauth2.AuthStyleInHeader)
273 // or use the "authFn" parameter in the related functions for custom needs.
274 func (r *ClientCredentialsRequest) Auth(req *http.Request) {
275 if r.ClientSecret != "" {
276 req.SetBasicAuth(url.QueryEscape(r.ClientID), url.QueryEscape(r.ClientSecret))
277 }
278 }