Class: Moloni::Auth
- Inherits:
-
Object
- Object
- Moloni::Auth
- Defined in:
- lib/moloni/auth.rb
Constant Summary collapse
- WEB_AUTH_URL =
'https://www.moloni.pt/ac/root/oauth/'- API_TOKEN_PATH =
'grant/'
Class Method Summary collapse
- .auth_url ⇒ Object
- .get_tokens(authorization_code) ⇒ Object
- .refresh_tokens(refresh_token = nil) ⇒ Object
Instance Method Summary collapse
- #auth_url ⇒ Object
- #get_access_token_url(authorization_code) ⇒ Object
- #get_token(authorization_code) ⇒ Object
-
#initialize ⇒ Auth
constructor
A new instance of Auth.
- #refresh_tokens(refresh_token = nil) ⇒ Object
- #refresh_tokens_url(refresh_token) ⇒ Object
- #token_full_uri ⇒ Object
Constructor Details
#initialize ⇒ Auth
Returns a new instance of Auth.
11 12 13 14 |
# File 'lib/moloni/auth.rb', line 11 def initialize @config = Moloni.config @api_domain = API_BASE_URL end |
Class Method Details
.auth_url ⇒ Object
16 17 18 |
# File 'lib/moloni/auth.rb', line 16 def self.auth_url new.auth_url end |
.get_tokens(authorization_code) ⇒ Object
38 39 40 |
# File 'lib/moloni/auth.rb', line 38 def self.get_tokens() new.get_token() end |
.refresh_tokens(refresh_token = nil) ⇒ Object
62 63 64 |
# File 'lib/moloni/auth.rb', line 62 def self.refresh_tokens(refresh_token = nil) new.refresh_tokens(refresh_token) end |
Instance Method Details
#auth_url ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/moloni/auth.rb', line 20 def auth_url uri = Addressable::URI.parse(WEB_AUTH_URL) query = { response_type: 'code', client_id: @config.developer_id, redirect_uri: @config.redirect_uri } uri.query_values = query Addressable::URI.unencode(uri.to_s) end |
#get_access_token_url(authorization_code) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/moloni/auth.rb', line 48 def get_access_token_url() uri = token_full_uri uri.query_values = { client_id: @config.developer_id, client_secret: @config.client_secret, code: , redirect_uri: @config.redirect_uri, grant_type: 'authorization_code' } Addressable::URI.unencode(uri.to_s) end |
#get_token(authorization_code) ⇒ Object
42 43 44 45 46 |
# File 'lib/moloni/auth.rb', line 42 def get_token() result = Faraday.get(get_access_token_url()) parse_and_update(result.body) end |
#refresh_tokens(refresh_token = nil) ⇒ Object
66 67 68 69 70 71 72 73 74 75 |
# File 'lib/moloni/auth.rb', line 66 def refresh_tokens(refresh_token = nil) token = refresh_token || @config.refresh_token raise TokenExpiredError, 'Refresh token is missing or expired' if token.nil? || token.empty? raise TokenExpiredError, 'Refresh token has expired' if @config.refresh_token_expired? result = Faraday.get(refresh_tokens_url(token)) parse_and_update(result.body) end |
#refresh_tokens_url(refresh_token) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/moloni/auth.rb', line 77 def refresh_tokens_url(refresh_token) uri = token_full_uri uri.query_values = { client_id: @config.developer_id, client_secret: @config.client_secret, refresh_token:, grant_type: 'refresh_token' } Addressable::URI.unencode(uri.to_s) end |
#token_full_uri ⇒ Object
34 35 36 |
# File 'lib/moloni/auth.rb', line 34 def token_full_uri Addressable::URI.join(API_BASE_URL, API_TOKEN_PATH) end |