|
5 | 5 |
|
6 | 6 | module OmniAuth |
7 | 7 | module Strategies |
| 8 | + # MLH OAuth2 Strategy |
| 9 | + # |
| 10 | + # @example Basic Usage |
| 11 | + # use OmniAuth::Builder do |
| 12 | + # provider :mlh, ENV['MLH_KEY'], ENV['MLH_SECRET'] |
| 13 | + # end |
| 14 | + # |
| 15 | + # @example With Expandable Fields |
| 16 | + # use OmniAuth::Builder do |
| 17 | + # provider :mlh, ENV['MLH_KEY'], ENV['MLH_SECRET'], |
| 18 | + # expand_fields: ['education', 'professional_experience'] |
| 19 | + # end |
| 20 | + # |
| 21 | + # @example With Refresh Tokens (offline access) |
| 22 | + # use OmniAuth::Builder do |
| 23 | + # provider :mlh, ENV['MLH_KEY'], ENV['MLH_SECRET'], |
| 24 | + # scope: 'user:read:profile offline_access' |
| 25 | + # end |
| 26 | + # |
| 27 | + # When offline_access scope is requested, the strategy will include |
| 28 | + # refresh_token in the credentials hash if provided by the server. |
8 | 29 | class MLH < OmniAuth::Strategies::OAuth2 # :nodoc: |
9 | 30 | option :name, :mlh |
10 | 31 |
|
11 | 32 | option :client_options, { |
12 | 33 | site: 'https://my.mlh.io', |
13 | 34 | authorize_url: 'oauth/authorize', |
14 | | - token_url: 'oauth/token' |
| 35 | + token_url: 'oauth/token', |
| 36 | + auth_scheme: :request_body # Change from basic auth to request body |
15 | 37 | } |
16 | 38 |
|
| 39 | + # Support expandable fields through options |
| 40 | + option :expand_fields, [] |
| 41 | + |
17 | 42 | uid { data[:id] } |
18 | 43 |
|
19 | 44 | info do |
20 | | - data.slice( |
21 | | - :email, |
22 | | - :created_at, |
23 | | - :updated_at, |
24 | | - :first_name, |
25 | | - :last_name, |
26 | | - :level_of_study, |
27 | | - :major, |
28 | | - :date_of_birth, |
29 | | - :gender, |
30 | | - :phone_number, |
31 | | - :profession_type, |
32 | | - :company_name, |
33 | | - :company_title, |
34 | | - :scopes, |
35 | | - :school |
36 | | - ) |
| 45 | + { |
| 46 | + # Basic fields |
| 47 | + id: data[:id], |
| 48 | + created_at: data[:created_at], |
| 49 | + updated_at: data[:updated_at], |
| 50 | + first_name: data[:first_name], |
| 51 | + last_name: data[:last_name], |
| 52 | + email: data[:email], |
| 53 | + phone_number: data[:phone_number], |
| 54 | + roles: data[:roles], |
| 55 | + |
| 56 | + # Expandable fields |
| 57 | + profile: data[:profile], |
| 58 | + address: data[:address], |
| 59 | + social_profiles: data[:social_profiles], |
| 60 | + professional_experience: data[:professional_experience], |
| 61 | + education: data[:education], |
| 62 | + identifiers: data[:identifiers] |
| 63 | + } |
37 | 64 | end |
38 | 65 |
|
39 | 66 | def data |
40 | | - @data ||= begin |
41 | | - access_token.get('/api/v3/user.json').parsed.deep_symbolize_keys[:data] |
42 | | - rescue StandardError |
43 | | - {} |
| 67 | + @data ||= fetch_and_process_data.compact |
| 68 | + rescue StandardError |
| 69 | + {} |
| 70 | + end |
| 71 | + |
| 72 | + private |
| 73 | + |
| 74 | + def fetch_and_process_data |
| 75 | + response = access_token.get(build_api_url) |
| 76 | + data = JSON.parse(response.body, symbolize_names: true) |
| 77 | + return {} unless data.is_a?(Hash) |
| 78 | + |
| 79 | + symbolize_nested_arrays(data) |
| 80 | + end |
| 81 | + |
| 82 | + def build_api_url |
| 83 | + url = 'https://api.mlh.com/v4/users/me' |
| 84 | + expand_fields = options[:expand_fields] || [] |
| 85 | + return url if expand_fields.empty? |
| 86 | + |
| 87 | + expand_query = expand_fields.map { |f| "expand[]=#{f}" }.join('&') |
| 88 | + "#{url}?#{expand_query}" |
| 89 | + end |
| 90 | + |
| 91 | + def symbolize_nested_arrays(hash) |
| 92 | + hash.transform_values do |value| |
| 93 | + case value |
| 94 | + when Hash |
| 95 | + symbolize_nested_arrays(value) |
| 96 | + when Array |
| 97 | + value.map { |item| item.is_a?(Hash) ? symbolize_nested_arrays(item) : item } |
| 98 | + else |
| 99 | + value |
| 100 | + end |
44 | 101 | end |
45 | 102 | end |
46 | 103 | end |
|
0 commit comments