Class: Shakha::Providers::Google
- 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
- #authorize_url(state:, code_challenge:, redirect_uri:, nonce: nil) ⇒ Object
- #exchange_code(code:, code_verifier:, redirect_uri:) ⇒ Object
- #identity_from_response(token_response, expected_nonce: nil) ⇒ Object
- #provider_name ⇒ Object
- #scopes ⇒ Object
Instance Method Details
#authorize_url(state:, code_challenge:, redirect_uri:, nonce: nil) ⇒ 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 (state:, code_challenge:, redirect_uri:, nonce: nil) 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: nonce } "#{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, expected_nonce: nil) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/shakha/providers/google.rb', line 46 def identity_from_response(token_response, expected_nonce: nil) id_token = token_response["id_token"] raise OAuthError, "No id_token received" unless id_token # Signature is not re-verified: the token comes directly from Google's # token endpoint over a TLS connection we initiated, so its provenance # is the transport. The claims below still need checking. payload = JWT.decode(id_token, nil, false)[0] verify_claims!(payload, expected_nonce) { provider: :google, uid: payload["sub"], email: payload["email"], name: payload["name"], picture: payload["picture"] } end |
#provider_name ⇒ Object
12 13 14 |
# File 'lib/shakha/providers/google.rb', line 12 def provider_name :google end |
#scopes ⇒ Object
65 66 67 |
# File 'lib/shakha/providers/google.rb', line 65 def scopes %w[openid email profile] end |