Class: Code0::Identities::Provider::BaseOauth

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

Direct Known Subclasses

Discord, Github, Gitlab, Google, Microsoft, Oidc

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_loader) ⇒ BaseOauth

Returns a new instance of BaseOauth.

Parameters:

  • config_loader: (Proc, Hash[Symbol, any])


9
10
11
# File 'lib/code0/identities/provider/base_oauth.rb', line 9

def initialize(config_loader)
  @config_loader = config_loader
end

Instance Attribute Details

#config_loaderProc, Hash[Symbol, any] (readonly)

Returns the value of attribute config_loader.

Returns:

  • (Proc, Hash[Symbol, any])


7
8
9
# File 'lib/code0/identities/provider/base_oauth.rb', line 7

def config_loader
  @config_loader
end

Instance Method Details

#access_token(code) ⇒ Array[String]

Parameters:

  • code: (String)

Returns:

  • (Array[String])


50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/code0/identities/provider/base_oauth.rb', line 50

def access_token(code)
  response = HTTParty.post(token_url,
                           body: URI.encode_www_form(token_payload(code)), headers: {
                             "Content-Type" => "application/x-www-form-urlencoded",
                             "Accept" => "application/json"
                           })

  check_response response

  parsed = response.parsed_response

  [parsed["access_token"], parsed["token_type"]]
end

#authorization_urlString

Returns:

  • (String)

Raises:

  • (NotImplementedError)


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

def authorization_url
  raise NotImplementedError
end

#check_response(response) ⇒ void

This method returns an undefined value.

Parameters:

  • response: (Net::HTTPResponse)

Raises:



64
65
66
67
68
# File 'lib/code0/identities/provider/base_oauth.rb', line 64

def check_response(response)
  return if response.code == 200

  raise Error, response.body
end

#configHash[Symbol, any]

Returns:

  • (Hash[Symbol, any])


74
75
76
77
78
79
80
81
# File 'lib/code0/identities/provider/base_oauth.rb', line 74

def config
  config = config_loader
  config = config_loader.call if config_loader.is_a?(Proc)

  config[:provider_name] ||= self.class.name.downcase.split("::").last

  config
end

#create_identity(response, token, token_type) ⇒ Identity

Parameters:

  • response: (Net::HTTPResponse)
  • token: (String)
  • token_type: (String)

Returns:

Raises:

  • (NotImplementedError)


70
71
72
# File 'lib/code0/identities/provider/base_oauth.rb', line 70

def create_identity(response, token, token_type)
  raise NotImplementedError
end

#load_identity(**params) ⇒ Identity

Parameters:

  • params: (Hash[Symbol, any])

Returns:



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/code0/identities/provider/base_oauth.rb', line 33

def load_identity(**params)
  code = params[:code]
  token, token_type = access_token code

  response = HTTParty.get(user_details_url,
                          headers: {
                            Authorization: "#{token_type} #{token}",
                            "Accept" => "application/json"
                          })

  check_response response

  create_identity response, token, token_type
end

#token_payload(code) ⇒ Hash[Symbol, any]

Parameters:

  • code: (String)

Returns:

  • (Hash[Symbol, any])

Raises:

  • (NotImplementedError)


29
30
31
# File 'lib/code0/identities/provider/base_oauth.rb', line 29

def token_payload(code)
  raise NotImplementedError
end

#token_urlString

Returns:

  • (String)

Raises:

  • (NotImplementedError)


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

def token_url
  raise NotImplementedError
end

#user_details_urlString

Returns:

  • (String)

Raises:

  • (NotImplementedError)


25
26
27
# File 'lib/code0/identities/provider/base_oauth.rb', line 25

def user_details_url
  raise NotImplementedError
end

#validate_config!void

This method returns an undefined value.

Raises:

  • (NotImplementedError)


13
14
15
# File 'lib/code0/identities/provider/base_oauth.rb', line 13

def validate_config!
  raise NotImplementedError
end