lib/omniauth/strategies/nexaas_id.rb in omniauth-nexaas_id-0.1.2 vs lib/omniauth/strategies/nexaas_id.rb in omniauth-nexaas_id-0.2.0
- old
+ new
@@ -12,24 +12,27 @@
super
end
option :name, :nexaas_id
option :client_options, site: 'https://id.nexaas.com'
+ option :list_emails, false
uid do
raw_info['id']
end
info do
+ main_email = raw_info['email']
{
id: raw_info['id'],
name: {
first: raw_info['first_name'],
last: raw_info['last_name']
},
fullname: raw_info['full_name'],
- email: raw_info['email'],
+ email: main_email,
+ emails: raw_info['emails'] || [main_email],
picture_url: raw_info['picture']
}
end
extra do
@@ -37,27 +40,12 @@
raw_info: raw_info,
legacy: { api_token: @api_token }
}
end
- # Example:
- #
- # {
- # "id"=>"e9fa918b-a90e-49f3-86ec-e3ce92488a3e",
- # "full_name"=>"John Doe",
- # "email"=>"john@doe.com",
- # "emails"=>[{"address"=>"john@doe.com", "confirmed"=>true}],
- # "created_at"=>"2016-07-21T22:02:17Z",
- # "updated_at"=>"2016-07-21T22:02:17Z",
- # "_links"=>{
- # "self"=>{
- # "href"=>"https://id.nexaas.com/api/v1/users/e9fa918b-a90e-49f3-86ec-e3ce92488a3e"
- # }
- # }
- # }
def raw_info
- @raw_info ||= access_token.get('/api/v1/profile').parsed
+ @raw_info ||= build_raw_info
end
def request_phase
options[:authorize_params][:scopes] = options['scope'] || DEFAULT_SCOPE
super
@@ -68,9 +56,30 @@
def build_access_token
if (token = super) && token.params
@api_token = token.params['api_token']
end
token
+ end
+
+ def build_raw_info
+ add_email_list_to(access_token.get('/api/v1/profile').parsed)
+ end
+
+ def add_email_list_to(acc)
+ acc['emails'] = retrieve_emails(acc['id'])
+ acc
+ end
+
+ def retrieve_emails(id)
+ return unless options[:list_emails] # guard: access endpoint only if allowed
+ emails = access_token.get('/api/v1/profile/emails').parsed
+ got = email['id']
+ raise "unexpected id #{got} retrieving e-mails for #{id}" unless got == id
+ emails['emails']
+
+ rescue StandardError => err
+ warn(err)
+ nil
end
end
end
end