Class: OAuth2c::ThreeLegged::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/oauth2c/three_legged.rb

Direct Known Subclasses

Grants::AuthorizationCode, Grants::Implicit

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(agent, state:, scope: []) ⇒ Base

Returns a new instance of Base.



24
25
26
27
28
# File 'lib/oauth2c/three_legged.rb', line 24

def initialize(agent, state:, scope: [])
  @agent = agent
  @state = state
  update_scope(scope)
end

Instance Attribute Details

#scopeObject (readonly)

Returns the value of attribute scope.



22
23
24
# File 'lib/oauth2c/three_legged.rb', line 22

def scope
  @scope
end

Instance Method Details

#authz_urlObject



34
35
36
# File 'lib/oauth2c/three_legged.rb', line 34

def authz_url
  @agent.authz_url(state: @state, scope: @scope, **authz_params)
end

#token(callback_url_or_params, fragment_params = nil) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/oauth2c/three_legged.rb', line 38

def token(callback_url_or_params, fragment_params = nil)
  case callback_url_or_params
  when String
    query_params, fragment_params = parse_callback_url(callback_url_or_params)
  when Hash
    query_params, fragment_params = callback_url_or_params, fragment_params
  else
    raise ArgumentError, "invalid arguments, expects URL or hash with params"
  end

  if query_params[:error]
    raise Error.new(query_params[:error], query_params[:error_description])
  end

  if query_params[:state] != @state
    raise InvalidStateError, "callback url state mismatch"
  end

  if block_given?
    yield(query_params, fragment_params)
  else
    ok, response = @agent.token(include_redirect_uri: true, **token_params(query_params))
    handle_token_response(ok, response)
  end
end

#update_scope(scope) ⇒ Object



30
31
32
# File 'lib/oauth2c/three_legged.rb', line 30

def update_scope(scope)
  @scope = scope.dup.freeze
end