· 8 years ago · Jan 14, 2018, 08:32 PM
1require 'omniauth/oauth'
2require 'multi_json'
3
4module OmniAuth
5 module Strategies
6 class IdNet < OmniAuth::Strategies::OAuth2
7
8 def initialize(app, api_key = nil, secret_key = nil, options = {}, &block)
9 client_options = {
10 :site => IDNET_PROVIDER_URL,
11 :authorize_url => "#{IDNET_PROVIDER_URL}/oauth/authorize",
12 :access_token_url => "#{IDNET_PROVIDER_URL}/oauth/token",
13 :header_format => 'OAuth %s'
14 }
15 super(app, :id_net, api_key, secret_key, client_options, &block)
16 end
17
18protected
19
20 def user_data
21 @data ||= MultiJson.decode(@access_token.get("/api/profile").body)
22 end
23
24 def request_phase
25 options[:response_type] ||= 'code'
26 super
27 end
28
29 def callback_phase
30 options[:grant_type] ||= 'authorization_code'
31 super
32 end
33
34 def user_hash
35 user_data
36 end
37
38 def auth_hash
39 user_data
40 OmniAuth::Utils.deep_merge(super, {
41 'uid' => user_data["_id"],
42 'first_name' => user_data['first_name'],
43 'last_name' => user_data['last_name'],
44 'account_id' => user_data['account_id'],
45 'email' => user_data['email'],
46 'language' => user_data['language'],
47 'nickname' => user_data['nickname'],
48 'gender' => user_data['gender'],
49 'location' => {
50 'street_address' => user_data['address']['street_address'],
51 'city' => user_data['city'],
52 'country' => user_data['country'],
53 'state_or_province' => user_data['state_or_province'],
54 'zipcode' => user_data['zipcode']
55 }
56 })
57 end
58 end
59 end
60end