Skip to content

Commit fe40c1c

Browse files
committed
Adds oauth2_metadata config option
1 parent 476196d commit fe40c1c

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
@@ -305,6 +305,23 @@ func (b *jwtAuthBackend) pathCallback(ctx context.Context, req *logical.Request,
305305
}
306306
}
307307

308+
// Also fetch any requested extra oauth2 metadata
309+
oauth2Metadata := make(map[string]string)
310+
for _, mdname := range role.Oauth2Metadata {
311+
var md string
312+
switch mdname {
313+
case "id_token":
314+
md = string(token.IDToken())
315+
case "refresh_token":
316+
md = string(token.RefreshToken())
317+
case "access_token":
318+
md = string(token.AccessToken())
319+
default:
320+
return logical.ErrorResponse(errLoginFailed + " Unrecognized oauth2 metadata name " + mdname), nil
321+
}
322+
oauth2Metadata[mdname] = md
323+
}
324+
308325
if role.VerboseOIDCLogging {
309326
if c, err := json.Marshal(allClaims); err == nil {
310327
b.Logger().Debug("OIDC provider response", "claims", string(c))
@@ -326,6 +343,9 @@ func (b *jwtAuthBackend) pathCallback(ctx context.Context, req *logical.Request,
326343
for k, v := range alias.Metadata {
327344
tokenMetadata[k] = v
328345
}
346+
for k, v := range oauth2Metadata {
347+
tokenMetadata["oauth2_" + k] = v
348+
}
329349

330350
auth := &logical.Auth{
331351
Policies: role.Policies,

path_oidc_test.go

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

816816
auth := resp.Auth
817817

818+
if auth != nil {
819+
// Can't predict the content of oauth2_id_token
820+
// so instead copy it. This does at least
821+
// verify that it is present because if not it
822+
// introduces an empty value into expected.
823+
expected.Metadata["oauth2_id_token"] =
824+
auth.Metadata["oauth2_id_token"]
825+
}
826+
818827
if !reflect.DeepEqual(auth, expected) {
819-
t.Fatalf("expected: %v, auth: %v", expected, resp)
828+
t.Fatalf("expected: %v, resp: %v", expected, resp)
820829
}
821830
}
822831
})
@@ -1588,6 +1597,7 @@ func getBackendAndServer(t *testing.T, boundCIDRs bool) (logical.Backend, logica
15881597
"/nested/secret_code": "bar",
15891598
"temperature": "76",
15901599
},
1600+
"oauth2_metadata": []string{"id_token"},
15911601
}
15921602

15931603
if boundCIDRs {

path_role.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@ Defaults to 60 (1 minute) if set to 0 and can be disabled if set to -1.`,
133133
Type: framework.TypeKVPairs,
134134
Description: `Mappings of claims (key) that will be copied to a metadata field (value)`,
135135
},
136+
"oauth2_metadata": {
137+
Type: framework.TypeCommaStringSlice,
138+
Description: `Comma-separated list of one or more of access_token, id_token, refresh_token to return in metadata`,
139+
},
136140
"user_claim": {
137141
Type: framework.TypeString,
138142
Description: `The claim to use for the Identity entity alias name`,
@@ -218,6 +222,7 @@ type jwtRole struct {
218222
BoundClaimsType string `json:"bound_claims_type"`
219223
BoundClaims map[string]interface{} `json:"bound_claims"`
220224
ClaimMappings map[string]string `json:"claim_mappings"`
225+
Oauth2Metadata []string `json:"oauth2_metadata"`
221226
UserClaim string `json:"user_claim"`
222227
GroupsClaim string `json:"groups_claim"`
223228
OIDCScopes []string `json:"oidc_scopes"`
@@ -326,6 +331,7 @@ func (b *jwtAuthBackend) pathRoleRead(ctx context.Context, req *logical.Request,
326331
"bound_claims_type": role.BoundClaimsType,
327332
"bound_claims": role.BoundClaims,
328333
"claim_mappings": role.ClaimMappings,
334+
"oauth2_metadata": role.Oauth2Metadata,
329335
"user_claim": role.UserClaim,
330336
"user_claim_json_pointer": role.UserClaimJSONPointer,
331337
"groups_claim": role.GroupsClaim,
@@ -518,6 +524,10 @@ func (b *jwtAuthBackend) pathRoleCreateUpdate(ctx context.Context, req *logical.
518524
role.ClaimMappings = claimMappings
519525
}
520526

527+
if oauth2Metadata, ok := data.GetOk("oauth2_metadata"); ok {
528+
role.Oauth2Metadata = oauth2Metadata.([]string)
529+
}
530+
521531
if userClaim, ok := data.GetOk("user_claim"); ok {
522532
role.UserClaim = userClaim.(string)
523533
}

path_role_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,7 @@ func TestPath_Read(t *testing.T) {
767767
"bound_claims_type": "string",
768768
"bound_claims": map[string]interface{}(nil),
769769
"claim_mappings": map[string]string(nil),
770+
"oauth2_metadata": []string(nil),
770771
"bound_subject": "testsub",
771772
"bound_audiences": []string{"vault"},
772773
"allowed_redirect_uris": []string{"http://127.0.0.1"},

0 commit comments

Comments
 (0)