Class: Worochi::OAuth
- Inherits:
-
Object
- Object
- Worochi::OAuth
- Defined in:
- lib/worochi/oauth.rb
Overview
Implements OAuth2 authorization code flow for obtaining user tokens.
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 the access token using the refresh token.
Constructor Details
#initialize(service, redirect_uri = nil) ⇒ OAuth
Returns a new instance of OAuth.
16 17 18 19 20 21 22 23 24 |
# File 'lib/worochi/oauth.rb', line 16 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
12 13 14 |
# File 'lib/worochi/oauth.rb', line 12 def client @client end |
#options ⇒ Hashie::Mash
OAuth2 options.
8 9 10 |
# File 'lib/worochi/oauth.rb', line 8 def end |
Instance Method Details
#flow_end(code) ⇒ Hashie::Mash Also known as: get_token
Retrieves the token using the temporary authorization code.
42 43 44 45 46 47 |
# File 'lib/worochi/oauth.rb', line 42 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.
30 31 32 33 34 35 36 |
# File 'lib/worochi/oauth.rb', line 30 def flow_start(state=nil) client.site = .site opts = { scope: scope } opts[:state] = state if state opts[:redirect_uri] = .redirect_uri if .redirect_uri client.auth_code.(opts) end |
#refresh!(hash) ⇒ Hashie::Mash
Refreshes the access token using the refresh token.
55 56 57 58 59 60 61 62 |
# File 'lib/worochi/oauth.rb', line 55 def refresh!(hash) token = OAuth2::AccessToken.from_hash(@client, hash) begin token.refresh! rescue OAuth2::Error end Hashie::Mash.new(token.to_hash) end |