|
| 1 | +# frozen_string_literal: true |
| 2 | +require 'jwt' # for token signature validation |
| 3 | +require 'omniauth' # to inherit from OmniAuth::Error |
| 4 | +require 'oauth2' # to rescue OAuth2::Error |
| 5 | + |
| 6 | +module OmniAuth |
| 7 | + module MicrosoftGraph |
| 8 | + # Verify user email domains to mitigate the nOAuth vulnerability |
| 9 | + # https://www.descope.com/blog/post/noauth |
| 10 | + # https://clerk.com/docs/authentication/social-connections/microsoft#stay-secure-against-the-n-o-auth-vulnerability |
| 11 | + OIDC_CONFIG_URL = 'https://login.microsoftonline.com/organizations/v2.0/.well-known/openid-configuration' |
| 12 | + |
| 13 | + class DomainVerificationError < OmniAuth::Error; end |
| 14 | + |
| 15 | + class DomainVerifier |
| 16 | + def self.verify!(auth_hash, access_token, options) |
| 17 | + new(auth_hash, access_token, options).verify! |
| 18 | + end |
| 19 | + |
| 20 | + def initialize(auth_hash, access_token, options) |
| 21 | + @email_domain = auth_hash['info']['email']&.split('@')&.last |
| 22 | + @upn_domain = auth_hash['extra']['raw_info']['userPrincipalName']&.split('@')&.last |
| 23 | + @access_token = access_token |
| 24 | + @id_token = access_token.params['id_token'] |
| 25 | + @skip_verification = options[:skip_domain_verification] |
| 26 | + end |
| 27 | + |
| 28 | + def verify! |
| 29 | + # The userPrincipalName property is mutable, but must always contain a |
| 30 | + # verified domain: |
| 31 | + # |
| 32 | + # "The general format is alias@domain, where domain must be present in |
| 33 | + # the tenant's collection of verified domains." |
| 34 | + # https://learn.microsoft.com/en-us/graph/api/resources/user?view=graph-rest-1.0 |
| 35 | + # |
| 36 | + # This means while it's not suitable for consistently identifying a user |
| 37 | + # (the domain might change), it is suitable for verifying membership in |
| 38 | + # a given domain. |
| 39 | + return true if email_domain == upn_domain || |
| 40 | + skip_verification == true || |
| 41 | + (skip_verification.is_a?(Array) && skip_verification.include?(email_domain)) || |
| 42 | + domain_verified_jwt_claim |
| 43 | + raise DomainVerificationError, verification_error_message |
| 44 | + end |
| 45 | + |
| 46 | + private |
| 47 | + |
| 48 | + attr_reader :access_token, |
| 49 | + :email_domain, |
| 50 | + :id_token, |
| 51 | + :permitted_domains, |
| 52 | + :skip_verification, |
| 53 | + :upn_domain |
| 54 | + |
| 55 | + # https://learn.microsoft.com/en-us/entra/identity-platform/optional-claims-reference |
| 56 | + # Microsoft offers an optional claim `xms_edov` that will indicate whether the |
| 57 | + # user's email domain is part of the organization's verified domains. This has to be |
| 58 | + # explicitly configured in the app registration. |
| 59 | + # |
| 60 | + # To get to it, we need to decode the ID token with the key material from Microsoft's |
| 61 | + # OIDC configuration endpoint, and inspect it for the claim in question. |
| 62 | + def domain_verified_jwt_claim |
| 63 | + oidc_config = access_token.get(OIDC_CONFIG_URL).parsed |
| 64 | + algorithms = oidc_config['id_token_signing_alg_values_supported'] |
| 65 | + keys = JWT::JWK::Set.new(access_token.get(oidc_config['jwks_uri']).parsed) |
| 66 | + decoded_token = JWT.decode(id_token, nil, true, algorithms: algorithms, jwks: keys) |
| 67 | + # https://github.com/MicrosoftDocs/azure-docs/issues/111425#issuecomment-1761043378 |
| 68 | + # Comments seemed to indicate the value is not consistent |
| 69 | + ['1', 1, 'true', true].include?(decoded_token.first['xms_edov']) |
| 70 | + rescue JWT::VerificationError, ::OAuth2::Error |
| 71 | + false |
| 72 | + end |
| 73 | + |
| 74 | + def verification_error_message |
| 75 | + <<~MSG |
| 76 | + The email domain '#{email_domain}' is not a verified domain for this Azure AD account. |
| 77 | + You can either: |
| 78 | + * Update the user's email to match the principal domain '#{upn_domain}' |
| 79 | + * Skip verification on the '#{email_domain}' domain (not recommended) |
| 80 | + * Disable verification with `skip_domain_verification: true` (NOT RECOMMENDED!) |
| 81 | + Refer to the README for more details. |
| 82 | + MSG |
| 83 | + end |
| 84 | + end |
| 85 | + end |
| 86 | +end |
0 commit comments