Class: Rack::OAuth2::Client

Inherits:
Object
  • Object
show all
Includes:
AttrOptional, AttrRequired
Defined in:
lib/rack/oauth2/client.rb,
lib/rack/oauth2/client/error.rb,
lib/rack/oauth2/client/grant.rb,
lib/rack/oauth2/client/grant/password.rb,
lib/rack/oauth2/client/grant/jwt_bearer.rb,
lib/rack/oauth2/client/grant/saml2_bearer.rb,
lib/rack/oauth2/client/grant/refresh_token.rb,
lib/rack/oauth2/client/grant/token_exchange.rb,
lib/rack/oauth2/client/grant/authorization_code.rb,
lib/rack/oauth2/client/grant/client_credentials.rb

Defined Under Namespace

Classes: Error, Grant

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Client

Returns a new instance of Client.



8
9
10
11
12
13
14
15
16
# File 'lib/rack/oauth2/client.rb', line 8

def initialize(attributes = {})
  (required_attributes + optional_attributes).each do |key|
    self.send :"#{key}=", attributes[key]
  end
  @grant = Grant::ClientCredentials.new
  @authorization_endpoint ||= '/oauth2/authorize'
  @token_endpoint ||= '/oauth2/token'
  attr_missing!
end

Instance Method Details

#access_token!(*args) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/rack/oauth2/client.rb', line 71

def access_token!(*args)
  headers, params, http_client, options = authenticated_context_from(*args)
  params[:scope] = Array(options.delete(:scope)).join(' ') if options[:scope].present?
  params.merge! @grant.as_json
  params.merge! options
  handle_response do
    http_client.post(
      absolute_uri_for(token_endpoint),
      Util.compact_hash(params),
      headers
    ) do |req|
      yield req if block_given?
    end
  end
end

#authorization_code=(code) ⇒ Object



28
29
30
31
32
33
# File 'lib/rack/oauth2/client.rb', line 28

def authorization_code=(code)
  @grant = Grant::AuthorizationCode.new(
    code: code,
    redirect_uri: self.redirect_uri
  )
end

#authorization_uri(params = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/rack/oauth2/client.rb', line 18

def authorization_uri(params = {})
  params[:redirect_uri] ||= self.redirect_uri
  params[:response_type] ||= :code
  params[:response_type] = Array(params[:response_type]).join(' ')
  params[:scope] = Array(params[:scope]).join(' ')
  Util.redirect_uri absolute_uri_for(authorization_endpoint), :query, params.merge(
    client_id: self.identifier
  )
end

#force_token_type!(token_type) ⇒ Object



67
68
69
# File 'lib/rack/oauth2/client.rb', line 67

def force_token_type!(token_type)
  @forced_token_type = token_type.to_s
end

#jwt_bearer=(assertion) ⇒ Object



48
49
50
51
52
# File 'lib/rack/oauth2/client.rb', line 48

def jwt_bearer=(assertion)
  @grant = Grant::JWTBearer.new(
    assertion: assertion
  )
end

#refresh_token=(token) ⇒ Object



42
43
44
45
46
# File 'lib/rack/oauth2/client.rb', line 42

def refresh_token=(token)
  @grant = Grant::RefreshToken.new(
    refresh_token: token
  )
end

#resource_owner_credentials=(credentials) ⇒ Object



35
36
37
38
39
40
# File 'lib/rack/oauth2/client.rb', line 35

def resource_owner_credentials=(credentials)
  @grant = Grant::Password.new(
    username: credentials.first,
    password: credentials.last
  )
end

#revoke!(*args) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/rack/oauth2/client.rb', line 87

def revoke!(*args)
  headers, params, http_client, options = authenticated_context_from(*args)

  params.merge! case
  when access_token = options.delete(:access_token)
    {
      token: access_token,
      token_type_hint: :access_token
    }
  when refresh_token = options.delete(:refresh_token)
    {
      token: refresh_token,
      token_type_hint: :refresh_token
    }
  when @grant.is_a?(Grant::RefreshToken)
    {
      token: @grant.refresh_token,
      token_type_hint: :refresh_token
    }
  when options[:token].blank?
    raise ArgumentError, 'One of "token", "access_token" and "refresh_token" is required'
  end
  params.merge! options

  handle_revocation_response do
    http_client.post(
      absolute_uri_for(revocation_endpoint),
      Util.compact_hash(params),
      headers
    ) do |req|
      yield req if block_given?
    end
  end
end

#saml2_bearer=(assertion) ⇒ Object



54
55
56
57
58
# File 'lib/rack/oauth2/client.rb', line 54

def saml2_bearer=(assertion)
  @grant = Grant::SAML2Bearer.new(
    assertion: assertion
  )
end

#subject_token=(subject_token, subject_token_type = URN::TokenType::JWT) ⇒ Object



60
61
62
63
64
65
# File 'lib/rack/oauth2/client.rb', line 60

def subject_token=(subject_token, subject_token_type = URN::TokenType::JWT)
  @grant = Grant::TokenExchange.new(
    subject_token: subject_token,
    subject_token_type: subject_token_type
  )
end