Class: Code0::Identities::Provider::Google

Inherits:
BaseOauth
  • Object
show all
Defined in:
lib/code0/identities/provider/google.rb,
sig/code0/identities/provider/google.rbs

Instance Attribute Summary

Attributes inherited from BaseOauth

#config_loader

Instance Method Summary collapse

Methods inherited from BaseOauth

#access_token, #check_response, #config, #initialize, #load_identity

Constructor Details

This class inherits a constructor from Code0::Identities::Provider::BaseOauth

Instance Method Details

#authorization_urlString

Returns:

  • (String)


39
40
41
42
43
# File 'lib/code0/identities/provider/google.rb', line 39

def authorization_url
  # rubocop:disable Layout/LineLength
  base_url + "/o/oauth2/v2/auth?client_id=#{config[:client_id]}&response_type=code&redirect_uri=#{URI.encode_www_form_component(config[:redirect_uri])}&scope=openid%20email%20profile"
  # rubocop:enable Layout/LineLength
end

#base_url"https://accounts.google.com"

Returns:

  • ("https://accounts.google.com")


17
18
19
# File 'lib/code0/identities/provider/google.rb', line 17

def base_url
  "https://accounts.google.com"
end

#create_identity(response) ⇒ Identity

Parameters:

  • response: (Net::HTTPResponse)

Returns:



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/code0/identities/provider/google.rb', line 45

def create_identity(response, *)
  body = response.parsed_response

  identifier = body["sub"]
  username = body["name"]
  email = body["email"]
  firstname = body["given_name"]
  lastname = body["family_name"]

  Identity.new(config[:provider_name], identifier, username, email, firstname, lastname)
end

#token_payload(code) ⇒ { code: String, grant_type: "authorization_code", redirect_uri: String, client_id: String, client_secret: String }

Parameters:

  • code: (String)

Returns:

  • ({ code: String, grant_type: "authorization_code", redirect_uri: String, client_id: String, client_secret: String })


25
26
27
28
29
30
31
32
33
# File 'lib/code0/identities/provider/google.rb', line 25

def token_payload(code)
  {
    code: code,
    grant_type: "authorization_code",
    redirect_uri: config[:redirect_uri],
    client_id: config[:client_id],
    client_secret: config[:client_secret]
  }
end

#token_url"https://oauth2.googleapis.com/token"

Returns:

  • ("https://oauth2.googleapis.com/token")


21
22
23
# File 'lib/code0/identities/provider/google.rb', line 21

def token_url
  "https://oauth2.googleapis.com/token"
end

#user_details_url"https://www.googleapis.com/oauth2/v3/userinfo"

Returns:

  • ("https://www.googleapis.com/oauth2/v3/userinfo")


35
36
37
# File 'lib/code0/identities/provider/google.rb', line 35

def user_details_url
  "https://www.googleapis.com/oauth2/v3/userinfo"
end

#validate_config!Object



7
8
9
10
11
12
13
14
15
# File 'lib/code0/identities/provider/google.rb', line 7

def validate_config!
  required_keys = %i[redirect_uri client_id client_secret]

  missing_keys = required_keys - config.keys
  invalid_keys = config.keys - required_keys - [:provider_name]

  raise MissingConfigurationError, "Missing: #{missing_keys.inspect}" if missing_keys.any?
  raise InvalidConfigurationError, "Invalid: #{invalid_keys.inspect}" if invalid_keys.any?
end