Class: Worochi::OAuth
- Inherits:
-
Object
- Object
- Worochi::OAuth
- Defined in:
- lib/worochi/oauth.rb
Overview
Implements OAuth2 authorization code flow for obtaining user tokens.
Constant Summary collapse
- INTERNAL_OPTS =
List of options that should not be sent with HTTP requests.
[:id_env, :secret_env, :token_url, :authorize_url, :site, :token_site, :service]
Instance Attribute Summary collapse
-
#client ⇒ OAuth2::Client
readonly
The OAuth2 client.
-
#options ⇒ Hashie::Mash
OAuth2 options.
Instance Method Summary collapse
-
#flow_end(code) ⇒ Hashie::Mash
(also: #get_token)
Retrieves the token using the temporary authorization code.
-
#flow_start(state = nil) ⇒ String
Returns the URL to start the authorization flow.
-
#initialize(service, redirect_uri = nil) ⇒ OAuth
constructor
A new instance of OAuth.
-
#refresh(hash) ⇒ Hashie::Mash
Refreshes an existing access token using the refresh token.
Constructor Details
#initialize(service, redirect_uri = nil) ⇒ OAuth
Returns a new instance of OAuth.
20 21 22 23 24 25 26 27 28 |
# File 'lib/worochi/oauth.rb', line 20 def initialize(service, redirect_uri=nil) = Worochi::Config.service_opts(service).oauth .service = service .redirect_uri = redirect_uri opts = { site: .site } opts[:authorize_url] = . if . opts[:token_url] = .token_url if .token_url @client = OAuth2::Client.new(id, secret, opts) end |
Instance Attribute Details
#client ⇒ OAuth2::Client (readonly)
The OAuth2 client
16 17 18 |
# File 'lib/worochi/oauth.rb', line 16 def client @client end |
#options ⇒ Hashie::Mash
OAuth2 options.
12 13 14 |
# File 'lib/worochi/oauth.rb', line 12 def end |
Instance Method Details
#flow_end(code) ⇒ Hashie::Mash Also known as: get_token
Retrieves the token using the temporary authorization code.
45 46 47 48 49 50 |
# File 'lib/worochi/oauth.rb', line 45 def flow_end(code) client.site = .token_site || .site opts = {} opts[:redirect_uri] = .redirect_uri if .redirect_uri Hashie::Mash.new(client.auth_code.get_token(code, opts).to_hash) end |
#flow_start(state = nil) ⇒ String
Returns the URL to start the authorization flow.
34 35 36 37 38 39 |
# File 'lib/worochi/oauth.rb', line 34 def flow_start(state=nil) client.site = .site opts = params opts[:state] = state if state client.auth_code.(opts) end |
#refresh(hash) ⇒ Hashie::Mash
Refreshes an existing access token using the refresh token.
56 57 58 59 60 61 62 63 |
# File 'lib/worochi/oauth.rb', line 56 def refresh(hash) token = OAuth2::AccessToken.from_hash(@client, hash) begin Hashie::Mash.new(token.refresh!.to_hash) rescue OAuth2::Error raise Worochi::Error, 'Invalid refresh token' end end |