Unverified Commit ea147590 authored by Povilas Versockas's avatar Povilas Versockas Committed by GitHub
Browse files

oidc: add configureable subject claim (#2255)

parent 588bb24c
Showing with 245 additions and 23 deletions
+245 -23
......@@ -14,6 +14,10 @@ message OIDC {
string redirect_url = 4 [ (validate.rules).string = {min_bytes : 1} ];
repeated string scopes = 5;
// The subject is mapped from the JWT token's email claim by default.
// Set this field to the JWT token's claim name to override the subject.
string subject_claim_name_override = 6;
}
message Config {
......
......@@ -31,6 +31,9 @@ type OIDC struct {
ClientSecret string `protobuf:"bytes,3,opt,name=client_secret,json=clientSecret,proto3" json:"client_secret,omitempty"`
RedirectUrl string `protobuf:"bytes,4,opt,name=redirect_url,json=redirectUrl,proto3" json:"redirect_url,omitempty"`
Scopes []string `protobuf:"bytes,5,rep,name=scopes,proto3" json:"scopes,omitempty"`
// The subject is mapped from the JWT token's email claim by default.
// Set this field to the JWT token's claim name to override the subject.
SubjectClaimNameOverride string `protobuf:"bytes,6,opt,name=subject_claim_name_override,json=subjectClaimNameOverride,proto3" json:"subject_claim_name_override,omitempty"`
}
func (x *OIDC) Reset() {
......@@ -100,6 +103,13 @@ func (x *OIDC) GetScopes() []string {
return nil
}
func (x *OIDC) GetSubjectClaimNameOverride() string {
if x != nil {
return x.SubjectClaimNameOverride
}
return ""
}
type Config struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
......@@ -193,7 +203,7 @@ var file_config_service_authn_v1_authn_proto_rawDesc = []byte{
0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x1e, 0x63, 0x6c, 0x75, 0x74, 0x63, 0x68, 0x2e, 0x63, 0x6f,
0x6e, 0x66, 0x69, 0x67, 0x2e, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x2e, 0x61, 0x75, 0x74,
0x68, 0x6e, 0x2e, 0x76, 0x31, 0x1a, 0x17, 0x76, 0x61, 0x6c, 0x69, 0x64, 0x61, 0x74, 0x65, 0x2f,
0x76, 0x61, 0x6c, 0x69, 0x64, 0x61, 0x74, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0xbf,
0x76, 0x61, 0x6c, 0x69, 0x64, 0x61, 0x74, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0xfe,
0x01, 0x0a, 0x04, 0x4f, 0x49, 0x44, 0x43, 0x12, 0x1f, 0x0a, 0x06, 0x69, 0x73, 0x73, 0x75, 0x65,
0x72, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x42, 0x07, 0xfa, 0x42, 0x04, 0x72, 0x02, 0x20, 0x01,
0x52, 0x06, 0x69, 0x73, 0x73, 0x75, 0x65, 0x72, 0x12, 0x24, 0x0a, 0x09, 0x63, 0x6c, 0x69, 0x65,
......@@ -206,23 +216,27 @@ var file_config_service_authn_v1_authn_proto_rawDesc = []byte{
0x28, 0x09, 0x42, 0x07, 0xfa, 0x42, 0x04, 0x72, 0x02, 0x20, 0x01, 0x52, 0x0b, 0x72, 0x65, 0x64,
0x69, 0x72, 0x65, 0x63, 0x74, 0x55, 0x72, 0x6c, 0x12, 0x16, 0x0a, 0x06, 0x73, 0x63, 0x6f, 0x70,
0x65, 0x73, 0x18, 0x05, 0x20, 0x03, 0x28, 0x09, 0x52, 0x06, 0x73, 0x63, 0x6f, 0x70, 0x65, 0x73,
0x22, 0xbf, 0x01, 0x0a, 0x06, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x12, 0x2e, 0x0a, 0x0e, 0x73,
0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x5f, 0x73, 0x65, 0x63, 0x72, 0x65, 0x74, 0x18, 0x01, 0x20,
0x01, 0x28, 0x09, 0x42, 0x07, 0xfa, 0x42, 0x04, 0x72, 0x02, 0x20, 0x01, 0x52, 0x0d, 0x73, 0x65,
0x73, 0x73, 0x69, 0x6f, 0x6e, 0x53, 0x65, 0x63, 0x72, 0x65, 0x74, 0x12, 0x3a, 0x0a, 0x04, 0x6f,
0x69, 0x64, 0x63, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x24, 0x2e, 0x63, 0x6c, 0x75, 0x74,
0x63, 0x68, 0x2e, 0x63, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x2e, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63,
0x65, 0x2e, 0x61, 0x75, 0x74, 0x68, 0x6e, 0x2e, 0x76, 0x31, 0x2e, 0x4f, 0x49, 0x44, 0x43, 0x48,
0x00, 0x52, 0x04, 0x6f, 0x69, 0x64, 0x63, 0x12, 0x41, 0x0a, 0x1d, 0x65, 0x6e, 0x61, 0x62, 0x6c,
0x65, 0x5f, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x5f, 0x74, 0x6f, 0x6b, 0x65, 0x6e, 0x5f,
0x63, 0x72, 0x65, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x18, 0x03, 0x20, 0x01, 0x28, 0x08, 0x52, 0x1a,
0x65, 0x6e, 0x61, 0x62, 0x6c, 0x65, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x54, 0x6f, 0x6b,
0x65, 0x6e, 0x43, 0x72, 0x65, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x42, 0x06, 0x0a, 0x04, 0x74, 0x79,
0x70, 0x65, 0x42, 0x44, 0x5a, 0x42, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d,
0x2f, 0x6c, 0x79, 0x66, 0x74, 0x2f, 0x63, 0x6c, 0x75, 0x74, 0x63, 0x68, 0x2f, 0x62, 0x61, 0x63,
0x6b, 0x65, 0x6e, 0x64, 0x2f, 0x61, 0x70, 0x69, 0x2f, 0x63, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x2f,
0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x2f, 0x61, 0x75, 0x74, 0x68, 0x6e, 0x2f, 0x76, 0x31,
0x3b, 0x61, 0x75, 0x74, 0x68, 0x6e, 0x76, 0x31, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
0x12, 0x3d, 0x0a, 0x1b, 0x73, 0x75, 0x62, 0x6a, 0x65, 0x63, 0x74, 0x5f, 0x63, 0x6c, 0x61, 0x69,
0x6d, 0x5f, 0x6e, 0x61, 0x6d, 0x65, 0x5f, 0x6f, 0x76, 0x65, 0x72, 0x72, 0x69, 0x64, 0x65, 0x18,
0x06, 0x20, 0x01, 0x28, 0x09, 0x52, 0x18, 0x73, 0x75, 0x62, 0x6a, 0x65, 0x63, 0x74, 0x43, 0x6c,
0x61, 0x69, 0x6d, 0x4e, 0x61, 0x6d, 0x65, 0x4f, 0x76, 0x65, 0x72, 0x72, 0x69, 0x64, 0x65, 0x22,
0xbf, 0x01, 0x0a, 0x06, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x12, 0x2e, 0x0a, 0x0e, 0x73, 0x65,
0x73, 0x73, 0x69, 0x6f, 0x6e, 0x5f, 0x73, 0x65, 0x63, 0x72, 0x65, 0x74, 0x18, 0x01, 0x20, 0x01,
0x28, 0x09, 0x42, 0x07, 0xfa, 0x42, 0x04, 0x72, 0x02, 0x20, 0x01, 0x52, 0x0d, 0x73, 0x65, 0x73,
0x73, 0x69, 0x6f, 0x6e, 0x53, 0x65, 0x63, 0x72, 0x65, 0x74, 0x12, 0x3a, 0x0a, 0x04, 0x6f, 0x69,
0x64, 0x63, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x24, 0x2e, 0x63, 0x6c, 0x75, 0x74, 0x63,
0x68, 0x2e, 0x63, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x2e, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65,
0x2e, 0x61, 0x75, 0x74, 0x68, 0x6e, 0x2e, 0x76, 0x31, 0x2e, 0x4f, 0x49, 0x44, 0x43, 0x48, 0x00,
0x52, 0x04, 0x6f, 0x69, 0x64, 0x63, 0x12, 0x41, 0x0a, 0x1d, 0x65, 0x6e, 0x61, 0x62, 0x6c, 0x65,
0x5f, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x5f, 0x74, 0x6f, 0x6b, 0x65, 0x6e, 0x5f, 0x63,
0x72, 0x65, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x18, 0x03, 0x20, 0x01, 0x28, 0x08, 0x52, 0x1a, 0x65,
0x6e, 0x61, 0x62, 0x6c, 0x65, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x54, 0x6f, 0x6b, 0x65,
0x6e, 0x43, 0x72, 0x65, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x42, 0x06, 0x0a, 0x04, 0x74, 0x79, 0x70,
0x65, 0x42, 0x44, 0x5a, 0x42, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f,
0x6c, 0x79, 0x66, 0x74, 0x2f, 0x63, 0x6c, 0x75, 0x74, 0x63, 0x68, 0x2f, 0x62, 0x61, 0x63, 0x6b,
0x65, 0x6e, 0x64, 0x2f, 0x61, 0x70, 0x69, 0x2f, 0x63, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x2f, 0x73,
0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x2f, 0x61, 0x75, 0x74, 0x68, 0x6e, 0x2f, 0x76, 0x31, 0x3b,
0x61, 0x75, 0x74, 0x68, 0x6e, 0x76, 0x31, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
}
var (
......
......@@ -100,6 +100,8 @@ func (m *OIDC) validate(all bool) error {
errors = append(errors, err)
}
// no validation rules for SubjectClaimNameOverride
if len(errors) > 0 {
return OIDCMultiError(errors)
}
......
......@@ -42,14 +42,26 @@ type MockOIDCProviderServer struct {
srv *httptest.Server
client *http.Client
email string
email string
groups []string
customClaim string
TokenCount int
}
type testIdTokenClaims struct {
*jwt.StandardClaims
Email string `json:"email"`
Email string `json:"email"`
Groups []string `json:"groups"`
CustomClaim string `json:"custom_claim"`
}
func (m *MockOIDCProviderServer) SetGroupClaim(groups []string) {
m.groups = groups
}
func (m *MockOIDCProviderServer) SetCustomClaim(customClaim string) {
m.customClaim = customClaim
}
func (m *MockOIDCProviderServer) Close() {
......@@ -73,7 +85,9 @@ func (m *MockOIDCProviderServer) handle(w http.ResponseWriter, r *http.Request)
Audience: "my_client_id",
ExpiresAt: math.MaxInt32,
},
Email: m.email,
Email: m.email,
Groups: m.groups,
CustomClaim: m.customClaim,
}
tok, err := jwt.NewWithClaims(jwt.SigningMethodRS256, claims).SignedString(m.Key)
......
......@@ -405,7 +405,10 @@ func NewOIDCProvider(ctx context.Context, config *authnv1.Config, tokenStorage S
if err := pClaims.Check("authorization_code"); err != nil {
return nil, err
}
claimsFromOIDCTokenFunc := DefaultClaimsFromOIDCToken
if c.SubjectClaimNameOverride != "" {
claimsFromOIDCTokenFunc = NewClaimsConfig(c.SubjectClaimNameOverride).ClaimsFromOIDCToken
}
p := &OIDCProvider{
providerAlias: alias,
provider: provider,
......@@ -413,7 +416,7 @@ func NewOIDCProvider(ctx context.Context, config *authnv1.Config, tokenStorage S
oauth2: oc,
httpClient: ctx.Value(oauth2.HTTPClient).(*http.Client),
sessionSecret: config.SessionSecret,
claimsFromOIDCToken: DefaultClaimsFromOIDCToken,
claimsFromOIDCToken: claimsFromOIDCTokenFunc,
tokenStorage: tokenStorage,
enableServiceTokenCreation: tokenStorage != nil && config.EnableServiceTokenCreation,
}
......@@ -421,6 +424,41 @@ func NewOIDCProvider(ctx context.Context, config *authnv1.Config, tokenStorage S
return p, nil
}
func NewClaimsConfig(subjectClaimName string) *ClaimsConfig {
return &ClaimsConfig{
subjectClaimName: subjectClaimName,
}
}
type ClaimsConfig struct {
subjectClaimName string
}
func (cc *ClaimsConfig) ClaimsFromOIDCToken(ctx context.Context, t *oidc.IDToken) (*Claims, error) {
claims := make(map[string]interface{})
if err := t.Claims(&claims); err != nil {
return nil, err
}
subjectInt, ok := claims[cc.subjectClaimName]
if !ok {
return nil, fmt.Errorf("claims do not contain %s field", cc.subjectClaimName)
}
subject, ok := subjectInt.(string)
if !ok {
return nil, fmt.Errorf("claims field %s does not contain string value", cc.subjectClaimName)
}
if subject == "" {
return nil, fmt.Errorf("claims field %s is empty", cc.subjectClaimName)
}
sc := oidcTokenToStandardClaims(t)
sc.Subject = subject
return &Claims{
StandardClaims: sc,
Groups: []string{""},
}, nil
}
func oidcTokenToStandardClaims(t *oidc.IDToken) *jwt.StandardClaims {
return &jwt.StandardClaims{
ExpiresAt: t.Expiry.Unix(),
......
......@@ -465,3 +465,130 @@ oidc:
err = claims.Check("authorization_code")
assert.NoError(t, err)
}
func TestConfigureableOIDCClaims(t *testing.T) {
cfg := &authnv1.Config{}
apimock.FromYAML(`
session_secret: this_is_my_secret
oidc:
issuer: http://foo.example.com
client_id: my_client_id
client_secret: my_client_secret
redirect_url: "http://localhost:12000/v1/authn/callback"
subject_claim_name_override: "email"
scopes:
- openid
- email
`, cfg)
email := "user@example.com"
mockprovider := authnmock.NewMockOIDCProviderServer(email)
defer mockprovider.Close()
ctx := context.WithValue(context.Background(), oauth2.HTTPClient, mockprovider.Client())
p, err := NewOIDCProvider(ctx, cfg, nil)
assert.NoError(t, err)
assert.NotNil(t, p)
token, err := p.Exchange(context.Background(), "aaa")
assert.NoError(t, err)
assert.NotNil(t, token)
c, err := p.Verify(context.Background(), token.AccessToken)
assert.NoError(t, err)
assert.NotNil(t, c)
assert.Equal(t, email, c.Subject)
}
func TestConfigureableOIDCClaimsNoFieldProducesError(t *testing.T) {
cfg := &authnv1.Config{}
apimock.FromYAML(`
session_secret: this_is_my_secret
oidc:
issuer: http://foo.example.com
client_id: my_client_id
client_secret: my_client_secret
redirect_url: "http://localhost:12000/v1/authn/callback"
subject_claim_name_override: "field_does_not_exist"
scopes:
- openid
- email
`, cfg)
email := "user@example.com"
mockprovider := authnmock.NewMockOIDCProviderServer(email)
defer mockprovider.Close()
ctx := context.WithValue(context.Background(), oauth2.HTTPClient, mockprovider.Client())
p, err := NewOIDCProvider(ctx, cfg, nil)
assert.NoError(t, err)
assert.NotNil(t, p)
_, err = p.Exchange(context.Background(), "aaa")
assert.Error(t, err)
assert.ErrorContains(t, err, "claims do not contain field_does_not_exist field")
}
func TestConfigureableOIDCClaimsWrongFieldTypeProducesError(t *testing.T) {
cfg := &authnv1.Config{}
apimock.FromYAML(`
session_secret: this_is_my_secret
oidc:
issuer: http://foo.example.com
client_id: my_client_id
client_secret: my_client_secret
redirect_url: "http://localhost:12000/v1/authn/callback"
subject_claim_name_override: "groups"
scopes:
- openid
- email
`, cfg)
email := "user@example.com"
mockprovider := authnmock.NewMockOIDCProviderServer(email)
mockprovider.SetGroupClaim([]string{"group1", "group2"})
defer mockprovider.Close()
ctx := context.WithValue(context.Background(), oauth2.HTTPClient, mockprovider.Client())
p, err := NewOIDCProvider(ctx, cfg, nil)
assert.NoError(t, err)
assert.NotNil(t, p)
_, err = p.Exchange(context.Background(), "aaa")
assert.Error(t, err)
assert.ErrorContains(t, err, "claims field groups does not contain string value")
}
func TestConfigureableOIDCClaimsEmptyFieldProducesError(t *testing.T) {
cfg := &authnv1.Config{}
apimock.FromYAML(`
session_secret: this_is_my_secret
oidc:
issuer: http://foo.example.com
client_id: my_client_id
client_secret: my_client_secret
redirect_url: "http://localhost:12000/v1/authn/callback"
subject_claim_name_override: "custom_claim"
scopes:
- openid
- email
`, cfg)
email := "user@example.com"
mockprovider := authnmock.NewMockOIDCProviderServer(email)
mockprovider.SetCustomClaim("")
defer mockprovider.Close()
ctx := context.WithValue(context.Background(), oauth2.HTTPClient, mockprovider.Client())
p, err := NewOIDCProvider(ctx, cfg, nil)
assert.NoError(t, err)
assert.NotNil(t, p)
_, err = p.Exchange(context.Background(), "aaa")
assert.Error(t, err)
assert.ErrorContains(t, err, "claims field custom_claim is empty")
}
......@@ -8596,6 +8596,9 @@ export namespace clutch {
 
/** OIDC scopes */
scopes?: (string[]|null);
/** OIDC subjectClaimNameOverride */
subjectClaimNameOverride?: (string|null);
}
 
/** Represents a OIDC. */
......@@ -8622,6 +8625,9 @@ export namespace clutch {
/** OIDC scopes. */
public scopes: string[];
 
/** OIDC subjectClaimNameOverride. */
public subjectClaimNameOverride: string;
/**
* Verifies a OIDC message.
* @param message Plain object to verify
......@@ -20633,6 +20633,7 @@ export const clutch = $root.clutch = (() => {
* @property {string|null} [clientSecret] OIDC clientSecret
* @property {string|null} [redirectUrl] OIDC redirectUrl
* @property {Array.<string>|null} [scopes] OIDC scopes
* @property {string|null} [subjectClaimNameOverride] OIDC subjectClaimNameOverride
*/
 
/**
......@@ -20691,6 +20692,14 @@ export const clutch = $root.clutch = (() => {
*/
OIDC.prototype.scopes = $util.emptyArray;
 
/**
* OIDC subjectClaimNameOverride.
* @member {string} subjectClaimNameOverride
* @memberof clutch.config.service.authn.v1.OIDC
* @instance
*/
OIDC.prototype.subjectClaimNameOverride = "";
/**
* Verifies a OIDC message.
* @function verify
......@@ -20721,6 +20730,9 @@ export const clutch = $root.clutch = (() => {
if (!$util.isString(message.scopes[i]))
return "scopes: string[] expected";
}
if (message.subjectClaimNameOverride != null && message.hasOwnProperty("subjectClaimNameOverride"))
if (!$util.isString(message.subjectClaimNameOverride))
return "subjectClaimNameOverride: string expected";
return null;
};
 
......@@ -20751,6 +20763,8 @@ export const clutch = $root.clutch = (() => {
for (let i = 0; i < object.scopes.length; ++i)
message.scopes[i] = String(object.scopes[i]);
}
if (object.subjectClaimNameOverride != null)
message.subjectClaimNameOverride = String(object.subjectClaimNameOverride);
return message;
};
 
......@@ -20774,6 +20788,7 @@ export const clutch = $root.clutch = (() => {
object.clientId = "";
object.clientSecret = "";
object.redirectUrl = "";
object.subjectClaimNameOverride = "";
}
if (message.issuer != null && message.hasOwnProperty("issuer"))
object.issuer = message.issuer;
......@@ -20788,6 +20803,8 @@ export const clutch = $root.clutch = (() => {
for (let j = 0; j < message.scopes.length; ++j)
object.scopes[j] = message.scopes[j];
}
if (message.subjectClaimNameOverride != null && message.hasOwnProperty("subjectClaimNameOverride"))
object.subjectClaimNameOverride = message.subjectClaimNameOverride;
return object;
};
 
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment