Class: TreatanyoneCommonApi::AuthZero

Inherits:
Object
  • Object
show all
Defined in:
lib/treatanyone_common_api/auth_zero.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user_id = nil) ⇒ AuthZero

Returns a new instance of AuthZero.



8
9
10
11
12
13
14
15
# File 'lib/treatanyone_common_api/auth_zero.rb', line 8

def initialize(user_id=nil)
  @user_id = user_id.to_s.gsub('auth0|', '') # remove the prefix if there's any
  @auth0_domain = ENV["AUTH0_DOMAIN"]
  @auth0_client_id = ENV["AUTH0_CLIENT_ID"]
  @auth0_client_secret = ENV["AUTH0_CLIENT_SECRET"]
  @auth0_connection = ENV["AUTH0_CONNECTION"]
  @auth0_logical_api = ENV['AUTH0_LOGICAL_API']
end

Instance Attribute Details

#auth0_client_idObject (readonly)

Returns the value of attribute auth0_client_id.



6
7
8
# File 'lib/treatanyone_common_api/auth_zero.rb', line 6

def auth0_client_id
  @auth0_client_id
end

#auth0_client_secretObject (readonly)

Returns the value of attribute auth0_client_secret.



6
7
8
# File 'lib/treatanyone_common_api/auth_zero.rb', line 6

def auth0_client_secret
  @auth0_client_secret
end

#auth0_connectionObject (readonly)

Returns the value of attribute auth0_connection.



6
7
8
# File 'lib/treatanyone_common_api/auth_zero.rb', line 6

def auth0_connection
  @auth0_connection
end

#auth0_domainObject (readonly)

Returns the value of attribute auth0_domain.



6
7
8
# File 'lib/treatanyone_common_api/auth_zero.rb', line 6

def auth0_domain
  @auth0_domain
end

#auth0_logical_apiObject (readonly)

Returns the value of attribute auth0_logical_api.



6
7
8
# File 'lib/treatanyone_common_api/auth_zero.rb', line 6

def auth0_logical_api
  @auth0_logical_api
end

Instance Method Details

#block_user(user_id, block: true) ⇒ Object

Raises:

  • (StandardError)


96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/treatanyone_common_api/auth_zero.rb', line 96

def block_user(user_id, block: true)
  user_id.gsub!('auth0|', '') # remove the prefix if there's any
  uri = URI.join(auth0_domain, "/api/v2/users/auth0%7C#{user_id}")

  response = Faraday.patch(uri) do |req|
    req.headers['Content-Type'] = 'application/json'
    req.headers['Authorization'] = "#{token['token_type']} #{token['access_token']}"

    body = {
      "client_id": auth0_client_id,
      "connection": auth0_connection,
      "blocked": block
    }

    req.body = body.to_json
  end

  return true if response.success?

  response_body = JSON.parse(response.body)
  raise StandardError.new(response_body['message'] || response_body['error'] || 'Update user failed.')
end

#create_user(first_name:, last_name:, email:, password:, phone: nil, username: nil, metadata: {}) ⇒ Object

Raises:

  • (StandardError)


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/treatanyone_common_api/auth_zero.rb', line 17

def create_user(first_name:, last_name:, email:, password:, phone: nil, username: nil, metadata: {})
  uri = URI.join(auth0_domain, "/dbconnections/signup")
  username = default_username if username.blank?
  full_name = "#{first_name} #{last_name}"

  response = Faraday.post(uri) do |req|
    req.headers['Content-Type'] = 'application/json'
    req.body = {
        "client_id": auth0_client_id,
        "connection": auth0_connection,
        "email": email,
        "username": username,
        "password": password,
        "name": full_name,
        "given_name": first_name,
        "family_name": last_name,
        "phone_number": phone,
        "user_metadata": 
      }.to_json
  end

  response = JSON.parse(response.body)

  user_id = response["_id"]

  error_message = response["error"] || response["message"] || response["description"]
  raise StandardError.new(error_message) if user_id.blank?

  { user_id: user_id, username: response["username"], email: response["email"] }
end

#delete_user(user_id) ⇒ Object

Raises:

  • (StandardError)


80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/treatanyone_common_api/auth_zero.rb', line 80

def delete_user(user_id)
  user_id.gsub!('auth0|', '') # remove the prefix if there's any
  uri = URI.join(auth0_domain, "/api/v2/users/auth0%7C#{user_id}")

  response = Faraday.delete(uri) do |req|
    req.headers['Content-Type'] = 'application/json'
    req.headers['Authorization'] = "#{token['token_type']} #{token['access_token']}"
    req.body = { "client_id": auth0_client_id, "connection": auth0_connection }.to_json
  end

  return true if response.success?

  response_body = JSON.parse(response.body)
  raise StandardError.new(response_body['message'] || response_body['error'] || 'Delete user failed.')
end

#email_verified?(email) ⇒ Boolean

Returns:

  • (Boolean)


144
145
146
147
148
# File 'lib/treatanyone_common_api/auth_zero.rb', line 144

def email_verified?(email)
  user = get_user_details(email)

  user.count > 0 ? user.first["email_verified"] : false
end

#get_tokenObject



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/treatanyone_common_api/auth_zero.rb', line 150

def get_token
  uri = URI.join(auth0_domain, "/oauth/token")

  response = Faraday.post(uri) do |req|
    req.headers['Content-Type'] = 'application/json'
    req.body = {
        "grant_type": "client_credentials",
        "client_id": auth0_client_id,
        "client_secret": auth0_client_secret,
        "audience": URI.join(auth0_domain, "/api/v2/")
      }.to_json
  end

  JSON.parse(response.body)
end

#get_user_by_id(user_id) ⇒ Object



179
180
181
182
183
184
185
186
# File 'lib/treatanyone_common_api/auth_zero.rb', line 179

def get_user_by_id(user_id)
  user_id.gsub!('auth0|', '') # remove the prefix if there's any
  uri = URI.join(auth0_domain, "/api/v2/users/auth0%7C#{user_id}")

  response = Faraday.get(uri, nil, authorization: "#{token['token_type']} #{token['access_token']}")

  response.body != [] ? JSON.parse(response.body) : response.body
end

#get_user_details(email) ⇒ Object



170
171
172
173
174
175
176
177
# File 'lib/treatanyone_common_api/auth_zero.rb', line 170

def get_user_details(email)
  email = email.gsub('+', '%2B').downcase
  uri = URI.join(auth0_domain, "/api/v2/users-by-email?email=#{email}")

  response = Faraday.get(uri, nil, authorization: "#{token['token_type']} #{token['access_token']}")

  response.body != [] ? JSON.parse(response.body) : response.body
end

#mfa_token(username, password) ⇒ Object

Raises:

  • (StandardError)


119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/treatanyone_common_api/auth_zero.rb', line 119

def mfa_token(username, password)
  uri = URI.join(auth0_domain, "/oauth/token")

  response = Faraday.post(uri) do |req|
    req.headers['Content-Type'] = 'application/json'
    req.body = {
        "grant_type": "http://auth0.com/oauth/grant-type/password-realm",
        "username": username,
        "password": password,
        "client_id": auth0_client_id,
        "client_secret": auth0_client_secret,
        "audience": auth0_logical_api,
        "realm": auth0_connection,
        "scope": "openid"
      }.to_json
  end

  response = JSON.parse(response.body)

  raise StandardError.new("MFA is not enabled.") if response['access_token'].present?
  raise StandardError.new(response['error_description']) if response['error'] != 'mfa_required'

  response['mfa_token'] if response['error'] == 'mfa_required'
end

#tokenObject



166
167
168
# File 'lib/treatanyone_common_api/auth_zero.rb', line 166

def token
  @token ||= get_token
end

#update_user(user_id, first_name: nil, last_name: nil, email: nil, password: nil, phone: nil, username: nil, metadata: {}) ⇒ Object

Warning, known errors:

  • Cannot update email and phone_number simultaneously

  • Cannot update password and email simultaneously

  • Cannot update phone_number for non-sms user

Raises:

  • (StandardError)


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/treatanyone_common_api/auth_zero.rb', line 52

def update_user(user_id, first_name: nil, last_name: nil, email: nil, password: nil, phone: nil, username: nil, metadata: {})
  user_id.gsub!('auth0|', '') # remove the prefix if there's any
  uri = URI.join(auth0_domain, "/api/v2/users/auth0%7C#{user_id}")
  full_name = "#{first_name} #{last_name}"

  response = Faraday.patch(uri) do |req|
    req.headers['Content-Type'] = 'application/json'
    req.headers['Authorization'] = "#{token['token_type']} #{token['access_token']}"

    body = { "client_id": auth0_client_id, "connection": auth0_connection }
    body["name"] = full_name if full_name.present?
    body["given_name"] = first_name if first_name.present?
    body["family_name"] = last_name if last_name.present?
    body["email"] = email if email.present?
    body["password"] = password if password.present?
    body["phone_number"] = phone if phone.present?
    body["username"] = username if username.present?
    body["user_metadata"] =  if .keys.count > 0

    req.body = body.to_json
  end

  return true if response.success?

  response_body = JSON.parse(response.body)
  raise StandardError.new(response_body['message'] || response_body['error'] || 'Update user failed.')
end

#user_metadata(id) ⇒ Object



188
189
190
191
# File 'lib/treatanyone_common_api/auth_zero.rb', line 188

def (id)
  response = get_user_by_id(id)
  response['user_metadata'] || {}
end