Class: Contrib::Auth::Provider::GoogleAuth

Inherits:
Object
  • Object
show all
Defined in:
lib/contrib/auth/provider/google_auth.rb

Constant Summary collapse

DEFAULT_BASE_ENDPOINT =
'https://identitytoolkit.googleapis.com'.freeze
PUBLIC_KEYS_URI =
'https://www.googleapis.com/robot/v1/metadata/x509/[email protected]'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(api_key, http_client = Faraday.new(DEFAULT_BASE_ENDPOINT)) ⇒ GoogleAuth

Returns a new instance of GoogleAuth.



10
11
12
13
# File 'lib/contrib/auth/provider/google_auth.rb', line 10

def initialize(api_key, http_client = Faraday.new(DEFAULT_BASE_ENDPOINT))
  @api_key       = api_key
  @http_client   = http_client
end

Instance Method Details

#certificatesObject



76
77
78
79
# File 'lib/contrib/auth/provider/google_auth.rb', line 76

def certificates
  response = @http_client.get(PUBLIC_KEYS_URI)
  JSON.parse(response.body)
end

#change_password(id_token, password) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/contrib/auth/provider/google_auth.rb', line 81

def change_password(id_token, password)
  response = @http_client.post('/v1/accounts:update') do |req|
    req.params[:key] = @api_key
    req.headers['Content-Type'] = 'application/json'

    req.body = JSON.generate(
      idToken: id_token,
      password: password,
      returnSecureToken: true,
    )
  end

  parsed_response = JSON.parse(response.body)

  Contrib::Auth::Provider::Responses::ChangePassword.new(
    id_token:       parsed_response['idToken'],
    refresh_token:  parsed_response['refreshToken'],
    expires_in:     parsed_response['expiresIn'],
    email:          parsed_response['email'],
  )
end

#reset_password(email_or_username) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/contrib/auth/provider/google_auth.rb', line 60

def reset_password(email_or_username)
  # TODO: X-Firebase-Locale optional header
  response = @http_client.post('/v1/accounts:sendOobCode') do |req|
    req.params[:key] = @api_key
    req.headers['Content-Type'] = 'application/json'

    req.body = JSON.generate(
      email: email_or_username,
      requestType: 'PASSWORD_RESET',
    )
  end

  response.success?
end

#sign_in_with_password(email_or_username, password) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/contrib/auth/provider/google_auth.rb', line 38

def (email_or_username, password)
  response = @http_client.post('/v1/accounts:signInWithPassword') do |req|
    req.params[:key] = @api_key
    req.headers['Content-Type'] = 'application/json'
    req.body = JSON.generate(
      email: email_or_username,
      password: password,
      returnSecureToken: true,
    )
  end

  parsed_response = JSON.parse(response.body)

  # TODO: Handle errors
  Contrib::Auth::Provider::Responses::SignInWithPassword.new(
    id_token:       parsed_response['idToken'],
    refresh_token:  parsed_response['refreshToken'],
    expires_in:     parsed_response['expiresIn'],
  )
end

#sign_up_with_email_and_password(email_or_username, password) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/contrib/auth/provider/google_auth.rb', line 16

def (email_or_username, password)
  response = @http_client.post('/v1/accounts:signUp') do |req|
    req.params[:key] = @api_key
    req.headers['Content-Type'] = 'application/json'
    req.body = JSON.generate(
      email: email_or_username,
      password: password,
      returnSecureToken: true,
    )
  end

  parsed_response = JSON.parse(response.body)

  # TODO: Handle errors
  Contrib::Auth::Provider::Responses::SignUpWithEmailAndPassword.new(
    id_token:       parsed_response['idToken'],
    refresh_token:  parsed_response['refreshToken'],
    expires_in:     parsed_response['expiresIn'],
  )
end