Class: Shakha::Providers::Google

Inherits:
Base
  • Object
show all
Defined in:
lib/shakha/providers/google.rb

Constant Summary collapse

AUTHORIZE_URL =
"https://accounts.google.com/o/oauth2/v2/auth"
TOKEN_URL =
"https://oauth2.googleapis.com/token"

Instance Method Summary collapse

Instance Method Details

#authorize_url(state:, code_challenge:, redirect_uri:) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/shakha/providers/google.rb', line 16

def authorize_url(state:, code_challenge:, redirect_uri:)
  params = {
    client_id: Shakha.config.google_client_id,
    redirect_uri: redirect_uri,
    response_type: "code",
    scope: scopes.join(" "),
    code_challenge: code_challenge,
    code_challenge_method: "S256",
    state: state,
    access_type: "offline",
    prompt: "consent",
    nonce: SecureRandom.urlsafe_base64(32)
  }

  "#{AUTHORIZE_URL}?#{URI.encode_www_form(params)}"
end

#exchange_code(code:, code_verifier:, redirect_uri:) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/shakha/providers/google.rb', line 33

def exchange_code(code:, code_verifier:, redirect_uri:)
  response = http_post(TOKEN_URL, {
    code: code,
    client_id: Shakha.config.google_client_id,
    client_secret: Shakha.config.google_client_secret,
    redirect_uri: redirect_uri,
    grant_type: "authorization_code",
    code_verifier: code_verifier
  })

  JSON.parse(response.body)
end

#identity_from_response(token_response) ⇒ Object

Raises:



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/shakha/providers/google.rb', line 46

def identity_from_response(token_response)
  id_token = token_response["id_token"]
  raise OAuthError, "No id_token received" unless id_token

  payload = JWT.decode(id_token, nil, false)[0]

  {
    provider: :google,
    uid: payload["sub"],
    email: payload["email"],
    name: payload["name"],
    picture: payload["picture"]
  }
end

#provider_nameObject



12
13
14
# File 'lib/shakha/providers/google.rb', line 12

def provider_name
  :google
end

#scopesObject



61
62
63
# File 'lib/shakha/providers/google.rb', line 61

def scopes
  %w[openid email profile]
end