Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions lib/omniauth/strategies/clever.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class Clever < OmniAuth::Strategies::OAuth2
:token_url => 'https://clever.com/oauth/tokens'
}

option :get_user_info, false
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


# This option bubbles up to the OmniAuth::Strategies::OAuth2
# when we call super in the callback_phase below.
# **State will still be verified** when login is initiated by the client.
Expand Down Expand Up @@ -45,18 +47,35 @@ def callback_phase
uid{ raw_info['data']['id'] }

info do
{ :user_type => raw_info['type'] }.merge! raw_info['data']
{ :user_type => raw_info['type'] }.merge(raw_info['data']).merge(raw_user_info['data'])
end

extra do
{
'raw_info' => raw_info
'raw_info' => raw_info,
'raw_user_info' => raw_user_info
}
end

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

def raw_user_info
return @raw_user_info if @raw_user_info

@raw_user_info = {}

if options.get_user_info
user_type = raw_info['type']
user_id = raw_info['data']['id']
if user_type && user_id
@raw_user_info = access_token.get("/v1.1/#{user_type}s/#{user_id}").parsed
end
end
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have exactly the same use case, I needed more info than what the /me endpoint provided. But instead of building a URL from parts, I used the links collection that comes in the JSON returned from the /me endpoint. (An example of the links collection is at https://dev.clever.com/instant-login/users)

What do you think of something like this? This approach lets the API tell us where the canonical endpoint is for the user versus building up a URL from parts.

  extra do
    {
      raw_info: me_attrs,
      canonical: canonical_attrs
    }
  end                                                                                                                                                                                                                                                                                                                            

  def me_attrs
    @me_attrs ||= access_token.get('/me').parsed
  end

  def canonical_attrs
    @canonical_attrs ||= access_token.get(canonical_endpoint).parsed
  end

  def canonical_endpoint
    me_link('canonical').fetch('uri', '/me')
  end

  def me_link(rel)
    me_attrs.fetch('links', []).detect { |link| link['rel'] == rel } || {}
  end


@raw_user_info
end
end
end
end