Skip to content

Commit d24f121

Browse files
committed
Adds oauth2_metadata config option
1 parent 7311fc7 commit d24f121

File tree

4 files changed

+42
-1
lines changed

4 files changed

+42
-1
lines changed

path_oidc.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,23 @@ func (b *jwtAuthBackend) pathCallback(ctx context.Context, req *logical.Request,
284284
}
285285
}
286286

287+
// Also fetch any requested extra oauth2 metadata
288+
oauth2Metadata := make(map[string]string)
289+
for _, mdname := range role.Oauth2Metadata {
290+
var md string
291+
switch mdname {
292+
case "id_token":
293+
md = string(token.IDToken())
294+
case "refresh_token":
295+
md = string(token.RefreshToken())
296+
case "access_token":
297+
md = string(token.AccessToken())
298+
default:
299+
return logical.ErrorResponse(errLoginFailed + " Unrecognized oauth2 metadata name " + mdname), nil
300+
}
301+
oauth2Metadata[mdname] = md
302+
}
303+
287304
if role.VerboseOIDCLogging {
288305
if c, err := json.Marshal(allClaims); err == nil {
289306
b.Logger().Debug("OIDC provider response", "claims", string(c))
@@ -305,6 +322,9 @@ func (b *jwtAuthBackend) pathCallback(ctx context.Context, req *logical.Request,
305322
for k, v := range alias.Metadata {
306323
tokenMetadata[k] = v
307324
}
325+
for k, v := range oauth2Metadata {
326+
tokenMetadata["oauth2_" + k] = v
327+
}
308328

309329
auth := &logical.Auth{
310330
Policies: role.Policies,

path_oidc_test.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -674,8 +674,17 @@ func TestOIDC_Callback(t *testing.T) {
674674

675675
auth := resp.Auth
676676

677+
if auth != nil {
678+
// Can't predict the content of oauth2_id_token
679+
// so instead copy it. This does at least
680+
// verify that it is present because if not it
681+
// introduces an empty value into expected.
682+
expected.Metadata["oauth2_id_token"] =
683+
auth.Metadata["oauth2_id_token"]
684+
}
685+
677686
if !reflect.DeepEqual(auth, expected) {
678-
t.Fatalf("expected: %v, auth: %v", expected, resp)
687+
t.Fatalf("expected: %v, resp: %v", expected, resp)
679688
}
680689
}
681690
})
@@ -1369,6 +1378,7 @@ func getBackendAndServer(t *testing.T, boundCIDRs bool) (logical.Backend, logica
13691378
"/nested/secret_code": "bar",
13701379
"temperature": "76",
13711380
},
1381+
"oauth2_metadata": []string{"id_token"},
13721382
}
13731383

13741384
if boundCIDRs {

path_role.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@ Defaults to 60 (1 minute) if set to 0 and can be disabled if set to -1.`,
121121
Type: framework.TypeKVPairs,
122122
Description: `Mappings of claims (key) that will be copied to a metadata field (value)`,
123123
},
124+
"oauth2_metadata": {
125+
Type: framework.TypeCommaStringSlice,
126+
Description: `Comma-separated list of one or more of access_token, id_token, refresh_token to return in metadata`,
127+
},
124128
"user_claim": {
125129
Type: framework.TypeString,
126130
Description: `The claim to use for the Identity entity alias name`,
@@ -201,6 +205,7 @@ type jwtRole struct {
201205
BoundClaimsType string `json:"bound_claims_type"`
202206
BoundClaims map[string]interface{} `json:"bound_claims"`
203207
ClaimMappings map[string]string `json:"claim_mappings"`
208+
Oauth2Metadata []string `json:"oauth2_metadata"`
204209
UserClaim string `json:"user_claim"`
205210
GroupsClaim string `json:"groups_claim"`
206211
OIDCScopes []string `json:"oidc_scopes"`
@@ -308,6 +313,7 @@ func (b *jwtAuthBackend) pathRoleRead(ctx context.Context, req *logical.Request,
308313
"bound_claims_type": role.BoundClaimsType,
309314
"bound_claims": role.BoundClaims,
310315
"claim_mappings": role.ClaimMappings,
316+
"oauth2_metadata": role.Oauth2Metadata,
311317
"user_claim": role.UserClaim,
312318
"groups_claim": role.GroupsClaim,
313319
"allowed_redirect_uris": role.AllowedRedirectURIs,
@@ -499,6 +505,10 @@ func (b *jwtAuthBackend) pathRoleCreateUpdate(ctx context.Context, req *logical.
499505
role.ClaimMappings = claimMappings
500506
}
501507

508+
if oauth2Metadata, ok := data.GetOk("oauth2_metadata"); ok {
509+
role.Oauth2Metadata = oauth2Metadata.([]string)
510+
}
511+
502512
if userClaim, ok := data.GetOk("user_claim"); ok {
503513
role.UserClaim = userClaim.(string)
504514
}

path_role_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,7 @@ func TestPath_Read(t *testing.T) {
762762
"bound_claims_type": "string",
763763
"bound_claims": map[string]interface{}(nil),
764764
"claim_mappings": map[string]string(nil),
765+
"oauth2_metadata": []string(nil),
765766
"bound_subject": "testsub",
766767
"bound_audiences": []string{"vault"},
767768
"allowed_redirect_uris": []string{"http://127.0.0.1"},

0 commit comments

Comments
 (0)