Class: HoninClient::Flow

Inherits:
Object
  • Object
show all
Defined in:
lib/honin/client/flow.rb

Instance Method Summary collapse

Constructor Details

#initialize(config = HoninClient.configuration) ⇒ Flow

Returns a new instance of Flow.



9
10
11
# File 'lib/honin/client/flow.rb', line 9

def initialize(config = HoninClient.configuration)
  @config = config
end

Instance Method Details

#authorize_url(state:, code_challenge:, scope: nil) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/honin/client/flow.rb', line 13

def authorize_url(state:, code_challenge:, scope: nil)
  uri = URI.parse(@config.authorize_url)
  uri.query = URI.encode_www_form(
    client_id: @config.client_id,
    redirect_uri: @config.redirect_uri,
    response_type: "code",
    code_challenge: code_challenge,
    code_challenge_method: "S256",
    scope: scope || @config.scope,
    state: state
  )
  uri.to_s
end

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



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/honin/client/flow.rb', line 27

def exchange_code(code:, code_verifier:, redirect_uri: nil)
  uri = URI.parse(@config.token_url)
  response = Net::HTTP.post_form(uri, {
    grant_type: "authorization_code",
    client_id: @config.client_id,
    client_secret: @config.client_secret,
    code: code,
    code_verifier: code_verifier,
    redirect_uri: redirect_uri || @config.redirect_uri
  })

  body = JSON.parse(response.body)

  if response.is_a?(Net::HTTPSuccess)
    HoninClient.token_verifier.verify(body["access_token"])
  else
    raise Error, "Token exchange failed: #{body["error"]}#{body["error_description"]}"
  end
rescue JSON::ParserError => e
  raise Error, "Invalid token response: #{e.message}"
end