Skip to content

Commit

Permalink
Parse info from JWT where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffcarbs committed May 25, 2016
1 parent 29232bb commit 56d2573
Showing 1 changed file with 37 additions and 4 deletions.
41 changes: 37 additions & 4 deletions lib/omniauth/strategies/office365.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,59 @@ class Office365 < OmniAuth::Strategies::OAuth2
# additional calls (if the user id is returned with the token
# or as a URI parameter). This may not be possible with all
# providers.
uid { raw_info['Id'] }
uid { parsed_uid }

# https://github.com/intridea/omniauth/wiki/Auth-Hash-Schema#schema-10-and-later
info do
{
:name => raw_info['DisplayName'],
:email => raw_info['EmailAddress'],
:nickname => raw_info['Alias']
:name => jwt_info['name'] || raw_info['DisplayName'],
:email => parsed_email,
:first_name => jwt_info['given_name'],
:last_name => jwt_info['family_name']
}
end

extra do
{
:jwt_info => jwt_info,
:raw_info => raw_info
}
end

def raw_info
@raw_info ||= access_token.get('api/v2.0/me').parsed
end

private

OFFICE365_JWT_USER_IDENTIFER = 'oid'.freeze
OFFICE365_JWT_ORGANIZATION_IDENTIFER = 'tid'.freeze
OFFICE365_JWT_USER_PRINCIPAL_NAME = 'upn'.freeze

def decoded_jwt_token
JWT.decode(access_token.token, nil, false)
rescue JWT::DecodeError
[]
end

def jwt_info
@jwt_info ||= decoded_jwt_token.first || {}
end

def parsed_email
return raw_info['EmailAddress'] if jwt_info.empty?

jwt_info[OFFICE365_JWT_USER_PRINCIPAL_NAME]
end

def parsed_uid
return raw_info['Id'] if jwt_info.empty?

[
jwt_info[OFFICE365_JWT_USER_IDENTIFER],
jwt_info[OFFICE365_JWT_ORGANIZATION_IDENTIFER]
].join('@')
end
end
end
end

0 comments on commit 56d2573

Please sign in to comment.