2021-05-05 05:06:05 +07:00
|
|
|
package validator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/url"
|
feat(oidc): add additional config options, accurate token times, and refactoring (#1991)
* This gives admins more control over their OIDC installation exposing options that had defaults before. Things like lifespans for authorize codes, access tokens, id tokens, refresh tokens, a option to enable the debug client messages, minimum parameter entropy. It also allows admins to configure the response modes.
* Additionally this records specific values about a users session indicating when they performed a specific authz factor so this is represented in the token accurately.
* Lastly we also implemented a OIDC key manager which calculates the kid for jwk's using the SHA1 digest instead of being static, or more specifically the first 7 chars. As per https://datatracker.ietf.org/doc/html/draft-ietf-jose-json-web-key#section-8.1.1 the kid should not exceed 8 chars. While it's allowed to exceed 8 chars, it must only be done so with a compelling reason, which we do not have.
2021-07-04 06:44:30 +07:00
|
|
|
"strings"
|
|
|
|
"time"
|
2021-05-05 05:06:05 +07:00
|
|
|
|
2021-08-11 08:04:35 +07:00
|
|
|
"github.com/authelia/authelia/v4/internal/configuration/schema"
|
|
|
|
"github.com/authelia/authelia/v4/internal/utils"
|
2021-05-05 05:06:05 +07:00
|
|
|
)
|
|
|
|
|
|
|
|
// ValidateIdentityProviders validates and update IdentityProviders configuration.
|
2022-02-28 10:15:01 +07:00
|
|
|
func ValidateIdentityProviders(config *schema.IdentityProvidersConfiguration, validator *schema.StructValidator) {
|
|
|
|
validateOIDC(config.OIDC, validator)
|
2021-05-05 05:06:05 +07:00
|
|
|
}
|
|
|
|
|
2022-02-28 10:15:01 +07:00
|
|
|
func validateOIDC(config *schema.OpenIDConnectConfiguration, validator *schema.StructValidator) {
|
|
|
|
if config != nil {
|
|
|
|
if config.IssuerPrivateKey == "" {
|
2021-08-03 16:55:21 +07:00
|
|
|
validator.Push(fmt.Errorf(errFmtOIDCNoPrivateKey))
|
2021-05-05 05:06:05 +07:00
|
|
|
}
|
|
|
|
|
2022-02-28 10:15:01 +07:00
|
|
|
if config.AccessTokenLifespan == time.Duration(0) {
|
|
|
|
config.AccessTokenLifespan = schema.DefaultOpenIDConnectConfiguration.AccessTokenLifespan
|
feat(oidc): add additional config options, accurate token times, and refactoring (#1991)
* This gives admins more control over their OIDC installation exposing options that had defaults before. Things like lifespans for authorize codes, access tokens, id tokens, refresh tokens, a option to enable the debug client messages, minimum parameter entropy. It also allows admins to configure the response modes.
* Additionally this records specific values about a users session indicating when they performed a specific authz factor so this is represented in the token accurately.
* Lastly we also implemented a OIDC key manager which calculates the kid for jwk's using the SHA1 digest instead of being static, or more specifically the first 7 chars. As per https://datatracker.ietf.org/doc/html/draft-ietf-jose-json-web-key#section-8.1.1 the kid should not exceed 8 chars. While it's allowed to exceed 8 chars, it must only be done so with a compelling reason, which we do not have.
2021-07-04 06:44:30 +07:00
|
|
|
}
|
|
|
|
|
2022-02-28 10:15:01 +07:00
|
|
|
if config.AuthorizeCodeLifespan == time.Duration(0) {
|
|
|
|
config.AuthorizeCodeLifespan = schema.DefaultOpenIDConnectConfiguration.AuthorizeCodeLifespan
|
feat(oidc): add additional config options, accurate token times, and refactoring (#1991)
* This gives admins more control over their OIDC installation exposing options that had defaults before. Things like lifespans for authorize codes, access tokens, id tokens, refresh tokens, a option to enable the debug client messages, minimum parameter entropy. It also allows admins to configure the response modes.
* Additionally this records specific values about a users session indicating when they performed a specific authz factor so this is represented in the token accurately.
* Lastly we also implemented a OIDC key manager which calculates the kid for jwk's using the SHA1 digest instead of being static, or more specifically the first 7 chars. As per https://datatracker.ietf.org/doc/html/draft-ietf-jose-json-web-key#section-8.1.1 the kid should not exceed 8 chars. While it's allowed to exceed 8 chars, it must only be done so with a compelling reason, which we do not have.
2021-07-04 06:44:30 +07:00
|
|
|
}
|
|
|
|
|
2022-02-28 10:15:01 +07:00
|
|
|
if config.IDTokenLifespan == time.Duration(0) {
|
|
|
|
config.IDTokenLifespan = schema.DefaultOpenIDConnectConfiguration.IDTokenLifespan
|
feat(oidc): add additional config options, accurate token times, and refactoring (#1991)
* This gives admins more control over their OIDC installation exposing options that had defaults before. Things like lifespans for authorize codes, access tokens, id tokens, refresh tokens, a option to enable the debug client messages, minimum parameter entropy. It also allows admins to configure the response modes.
* Additionally this records specific values about a users session indicating when they performed a specific authz factor so this is represented in the token accurately.
* Lastly we also implemented a OIDC key manager which calculates the kid for jwk's using the SHA1 digest instead of being static, or more specifically the first 7 chars. As per https://datatracker.ietf.org/doc/html/draft-ietf-jose-json-web-key#section-8.1.1 the kid should not exceed 8 chars. While it's allowed to exceed 8 chars, it must only be done so with a compelling reason, which we do not have.
2021-07-04 06:44:30 +07:00
|
|
|
}
|
|
|
|
|
2022-02-28 10:15:01 +07:00
|
|
|
if config.RefreshTokenLifespan == time.Duration(0) {
|
|
|
|
config.RefreshTokenLifespan = schema.DefaultOpenIDConnectConfiguration.RefreshTokenLifespan
|
feat(oidc): add additional config options, accurate token times, and refactoring (#1991)
* This gives admins more control over their OIDC installation exposing options that had defaults before. Things like lifespans for authorize codes, access tokens, id tokens, refresh tokens, a option to enable the debug client messages, minimum parameter entropy. It also allows admins to configure the response modes.
* Additionally this records specific values about a users session indicating when they performed a specific authz factor so this is represented in the token accurately.
* Lastly we also implemented a OIDC key manager which calculates the kid for jwk's using the SHA1 digest instead of being static, or more specifically the first 7 chars. As per https://datatracker.ietf.org/doc/html/draft-ietf-jose-json-web-key#section-8.1.1 the kid should not exceed 8 chars. While it's allowed to exceed 8 chars, it must only be done so with a compelling reason, which we do not have.
2021-07-04 06:44:30 +07:00
|
|
|
}
|
|
|
|
|
2022-02-28 10:15:01 +07:00
|
|
|
if config.MinimumParameterEntropy != 0 && config.MinimumParameterEntropy < 8 {
|
|
|
|
validator.PushWarning(fmt.Errorf(errFmtOIDCServerInsecureParameterEntropy, config.MinimumParameterEntropy))
|
feat(oidc): add additional config options, accurate token times, and refactoring (#1991)
* This gives admins more control over their OIDC installation exposing options that had defaults before. Things like lifespans for authorize codes, access tokens, id tokens, refresh tokens, a option to enable the debug client messages, minimum parameter entropy. It also allows admins to configure the response modes.
* Additionally this records specific values about a users session indicating when they performed a specific authz factor so this is represented in the token accurately.
* Lastly we also implemented a OIDC key manager which calculates the kid for jwk's using the SHA1 digest instead of being static, or more specifically the first 7 chars. As per https://datatracker.ietf.org/doc/html/draft-ietf-jose-json-web-key#section-8.1.1 the kid should not exceed 8 chars. While it's allowed to exceed 8 chars, it must only be done so with a compelling reason, which we do not have.
2021-07-04 06:44:30 +07:00
|
|
|
}
|
|
|
|
|
2022-02-28 10:15:01 +07:00
|
|
|
validateOIDCClients(config, validator)
|
2021-05-05 05:06:05 +07:00
|
|
|
|
2022-02-28 10:15:01 +07:00
|
|
|
if len(config.Clients) == 0 {
|
2021-08-03 16:55:21 +07:00
|
|
|
validator.Push(fmt.Errorf(errFmtOIDCNoClientsConfigured))
|
2021-05-05 05:06:05 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-28 10:15:01 +07:00
|
|
|
func validateOIDCClients(config *schema.OpenIDConnectConfiguration, validator *schema.StructValidator) {
|
2021-05-05 05:06:05 +07:00
|
|
|
invalidID, duplicateIDs := false, false
|
|
|
|
|
|
|
|
var ids []string
|
|
|
|
|
2022-02-28 10:15:01 +07:00
|
|
|
for c, client := range config.Clients {
|
2021-05-05 05:06:05 +07:00
|
|
|
if client.ID == "" {
|
|
|
|
invalidID = true
|
|
|
|
} else {
|
|
|
|
if client.Description == "" {
|
2022-02-28 10:15:01 +07:00
|
|
|
config.Clients[c].Description = client.ID
|
2021-05-05 05:06:05 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
if utils.IsStringInSliceFold(client.ID, ids) {
|
|
|
|
duplicateIDs = true
|
|
|
|
}
|
|
|
|
ids = append(ids, client.ID)
|
|
|
|
}
|
|
|
|
|
2021-07-15 18:02:03 +07:00
|
|
|
if client.Public {
|
|
|
|
if client.Secret != "" {
|
|
|
|
validator.Push(fmt.Errorf(errFmtOIDCClientPublicInvalidSecret, client.ID))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if client.Secret == "" {
|
2021-08-03 16:55:21 +07:00
|
|
|
validator.Push(fmt.Errorf(errFmtOIDCClientInvalidSecret, client.ID))
|
2021-07-15 18:02:03 +07:00
|
|
|
}
|
2021-05-05 05:06:05 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
if client.Policy == "" {
|
2022-02-28 10:15:01 +07:00
|
|
|
config.Clients[c].Policy = schema.DefaultOpenIDConnectClientConfiguration.Policy
|
2021-08-03 16:55:21 +07:00
|
|
|
} else if client.Policy != policyOneFactor && client.Policy != policyTwoFactor {
|
|
|
|
validator.Push(fmt.Errorf(errFmtOIDCClientInvalidPolicy, client.ID, client.Policy))
|
2021-05-05 05:06:05 +07:00
|
|
|
}
|
|
|
|
|
2022-02-28 10:15:01 +07:00
|
|
|
validateOIDCClientScopes(c, config, validator)
|
|
|
|
validateOIDCClientGrantTypes(c, config, validator)
|
|
|
|
validateOIDCClientResponseTypes(c, config, validator)
|
|
|
|
validateOIDCClientResponseModes(c, config, validator)
|
|
|
|
validateOIDDClientUserinfoAlgorithm(c, config, validator)
|
2021-05-05 05:06:05 +07:00
|
|
|
|
|
|
|
validateOIDCClientRedirectURIs(client, validator)
|
|
|
|
}
|
|
|
|
|
|
|
|
if invalidID {
|
2021-08-03 16:55:21 +07:00
|
|
|
validator.Push(fmt.Errorf(errFmtOIDCClientsWithEmptyID))
|
2021-05-05 05:06:05 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
if duplicateIDs {
|
2021-08-03 16:55:21 +07:00
|
|
|
validator.Push(fmt.Errorf(errFmtOIDCClientsDuplicateID))
|
2021-05-05 05:06:05 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
feat(oidc): add additional config options, accurate token times, and refactoring (#1991)
* This gives admins more control over their OIDC installation exposing options that had defaults before. Things like lifespans for authorize codes, access tokens, id tokens, refresh tokens, a option to enable the debug client messages, minimum parameter entropy. It also allows admins to configure the response modes.
* Additionally this records specific values about a users session indicating when they performed a specific authz factor so this is represented in the token accurately.
* Lastly we also implemented a OIDC key manager which calculates the kid for jwk's using the SHA1 digest instead of being static, or more specifically the first 7 chars. As per https://datatracker.ietf.org/doc/html/draft-ietf-jose-json-web-key#section-8.1.1 the kid should not exceed 8 chars. While it's allowed to exceed 8 chars, it must only be done so with a compelling reason, which we do not have.
2021-07-04 06:44:30 +07:00
|
|
|
func validateOIDCClientScopes(c int, configuration *schema.OpenIDConnectConfiguration, validator *schema.StructValidator) {
|
|
|
|
if len(configuration.Clients[c].Scopes) == 0 {
|
|
|
|
configuration.Clients[c].Scopes = schema.DefaultOpenIDConnectClientConfiguration.Scopes
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !utils.IsStringInSlice("openid", configuration.Clients[c].Scopes) {
|
|
|
|
configuration.Clients[c].Scopes = append(configuration.Clients[c].Scopes, "openid")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, scope := range configuration.Clients[c].Scopes {
|
2021-07-10 11:56:33 +07:00
|
|
|
if !utils.IsStringInSlice(scope, validOIDCScopes) {
|
feat(oidc): add additional config options, accurate token times, and refactoring (#1991)
* This gives admins more control over their OIDC installation exposing options that had defaults before. Things like lifespans for authorize codes, access tokens, id tokens, refresh tokens, a option to enable the debug client messages, minimum parameter entropy. It also allows admins to configure the response modes.
* Additionally this records specific values about a users session indicating when they performed a specific authz factor so this is represented in the token accurately.
* Lastly we also implemented a OIDC key manager which calculates the kid for jwk's using the SHA1 digest instead of being static, or more specifically the first 7 chars. As per https://datatracker.ietf.org/doc/html/draft-ietf-jose-json-web-key#section-8.1.1 the kid should not exceed 8 chars. While it's allowed to exceed 8 chars, it must only be done so with a compelling reason, which we do not have.
2021-07-04 06:44:30 +07:00
|
|
|
validator.Push(fmt.Errorf(
|
2022-02-28 10:15:01 +07:00
|
|
|
errFmtOIDCClientInvalidEntry,
|
|
|
|
configuration.Clients[c].ID, "scopes", strings.Join(validOIDCScopes, "', '"), scope))
|
feat(oidc): add additional config options, accurate token times, and refactoring (#1991)
* This gives admins more control over their OIDC installation exposing options that had defaults before. Things like lifespans for authorize codes, access tokens, id tokens, refresh tokens, a option to enable the debug client messages, minimum parameter entropy. It also allows admins to configure the response modes.
* Additionally this records specific values about a users session indicating when they performed a specific authz factor so this is represented in the token accurately.
* Lastly we also implemented a OIDC key manager which calculates the kid for jwk's using the SHA1 digest instead of being static, or more specifically the first 7 chars. As per https://datatracker.ietf.org/doc/html/draft-ietf-jose-json-web-key#section-8.1.1 the kid should not exceed 8 chars. While it's allowed to exceed 8 chars, it must only be done so with a compelling reason, which we do not have.
2021-07-04 06:44:30 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateOIDCClientGrantTypes(c int, configuration *schema.OpenIDConnectConfiguration, validator *schema.StructValidator) {
|
|
|
|
if len(configuration.Clients[c].GrantTypes) == 0 {
|
|
|
|
configuration.Clients[c].GrantTypes = schema.DefaultOpenIDConnectClientConfiguration.GrantTypes
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, grantType := range configuration.Clients[c].GrantTypes {
|
|
|
|
if !utils.IsStringInSlice(grantType, validOIDCGrantTypes) {
|
|
|
|
validator.Push(fmt.Errorf(
|
2022-02-28 10:15:01 +07:00
|
|
|
errFmtOIDCClientInvalidEntry,
|
|
|
|
configuration.Clients[c].ID, "grant_types", strings.Join(validOIDCGrantTypes, "', '"), grantType))
|
feat(oidc): add additional config options, accurate token times, and refactoring (#1991)
* This gives admins more control over their OIDC installation exposing options that had defaults before. Things like lifespans for authorize codes, access tokens, id tokens, refresh tokens, a option to enable the debug client messages, minimum parameter entropy. It also allows admins to configure the response modes.
* Additionally this records specific values about a users session indicating when they performed a specific authz factor so this is represented in the token accurately.
* Lastly we also implemented a OIDC key manager which calculates the kid for jwk's using the SHA1 digest instead of being static, or more specifically the first 7 chars. As per https://datatracker.ietf.org/doc/html/draft-ietf-jose-json-web-key#section-8.1.1 the kid should not exceed 8 chars. While it's allowed to exceed 8 chars, it must only be done so with a compelling reason, which we do not have.
2021-07-04 06:44:30 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateOIDCClientResponseTypes(c int, configuration *schema.OpenIDConnectConfiguration, _ *schema.StructValidator) {
|
|
|
|
if len(configuration.Clients[c].ResponseTypes) == 0 {
|
|
|
|
configuration.Clients[c].ResponseTypes = schema.DefaultOpenIDConnectClientConfiguration.ResponseTypes
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateOIDCClientResponseModes(c int, configuration *schema.OpenIDConnectConfiguration, validator *schema.StructValidator) {
|
|
|
|
if len(configuration.Clients[c].ResponseModes) == 0 {
|
|
|
|
configuration.Clients[c].ResponseModes = schema.DefaultOpenIDConnectClientConfiguration.ResponseModes
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, responseMode := range configuration.Clients[c].ResponseModes {
|
|
|
|
if !utils.IsStringInSlice(responseMode, validOIDCResponseModes) {
|
|
|
|
validator.Push(fmt.Errorf(
|
2022-02-28 10:15:01 +07:00
|
|
|
errFmtOIDCClientInvalidEntry,
|
|
|
|
configuration.Clients[c].ID, "response_modes", strings.Join(validOIDCResponseModes, "', '"), responseMode))
|
feat(oidc): add additional config options, accurate token times, and refactoring (#1991)
* This gives admins more control over their OIDC installation exposing options that had defaults before. Things like lifespans for authorize codes, access tokens, id tokens, refresh tokens, a option to enable the debug client messages, minimum parameter entropy. It also allows admins to configure the response modes.
* Additionally this records specific values about a users session indicating when they performed a specific authz factor so this is represented in the token accurately.
* Lastly we also implemented a OIDC key manager which calculates the kid for jwk's using the SHA1 digest instead of being static, or more specifically the first 7 chars. As per https://datatracker.ietf.org/doc/html/draft-ietf-jose-json-web-key#section-8.1.1 the kid should not exceed 8 chars. While it's allowed to exceed 8 chars, it must only be done so with a compelling reason, which we do not have.
2021-07-04 06:44:30 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-10 11:56:33 +07:00
|
|
|
func validateOIDDClientUserinfoAlgorithm(c int, configuration *schema.OpenIDConnectConfiguration, validator *schema.StructValidator) {
|
|
|
|
if configuration.Clients[c].UserinfoSigningAlgorithm == "" {
|
|
|
|
configuration.Clients[c].UserinfoSigningAlgorithm = schema.DefaultOpenIDConnectClientConfiguration.UserinfoSigningAlgorithm
|
|
|
|
} else if !utils.IsStringInSlice(configuration.Clients[c].UserinfoSigningAlgorithm, validOIDCUserinfoAlgorithms) {
|
2021-08-03 16:55:21 +07:00
|
|
|
validator.Push(fmt.Errorf(errFmtOIDCClientInvalidUserinfoAlgorithm,
|
2022-02-28 10:15:01 +07:00
|
|
|
configuration.Clients[c].ID, strings.Join(validOIDCUserinfoAlgorithms, ", "), configuration.Clients[c].UserinfoSigningAlgorithm))
|
2021-07-10 11:56:33 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-05 05:06:05 +07:00
|
|
|
func validateOIDCClientRedirectURIs(client schema.OpenIDConnectClientConfiguration, validator *schema.StructValidator) {
|
|
|
|
for _, redirectURI := range client.RedirectURIs {
|
2021-07-15 18:02:03 +07:00
|
|
|
if redirectURI == oauth2InstalledApp {
|
|
|
|
if client.Public {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-02-28 10:15:01 +07:00
|
|
|
validator.Push(fmt.Errorf(errFmtOIDCClientRedirectURIPublic, client.ID, oauth2InstalledApp))
|
2021-05-05 05:06:05 +07:00
|
|
|
|
2021-07-15 18:02:03 +07:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
parsedURL, err := url.Parse(redirectURI)
|
2021-05-05 05:06:05 +07:00
|
|
|
if err != nil {
|
2021-08-03 16:55:21 +07:00
|
|
|
validator.Push(fmt.Errorf(errFmtOIDCClientRedirectURICantBeParsed, client.ID, redirectURI, err))
|
2021-07-15 18:02:03 +07:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if !parsedURL.IsAbs() {
|
|
|
|
validator.Push(fmt.Errorf(errFmtOIDCClientRedirectURIAbsolute, client.ID, redirectURI))
|
|
|
|
return
|
2021-05-05 05:06:05 +07:00
|
|
|
}
|
|
|
|
|
2022-01-21 18:05:53 +07:00
|
|
|
if !client.Public && parsedURL.Scheme != schemeHTTPS && parsedURL.Scheme != schemeHTTP {
|
2021-08-03 16:55:21 +07:00
|
|
|
validator.Push(fmt.Errorf(errFmtOIDCClientRedirectURI, client.ID, redirectURI, parsedURL.Scheme))
|
2021-05-05 05:06:05 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|