Class: Gini::Api::OAuth

Inherits:
Object
  • Object
show all
Defined in:
lib/gini-api/oauth.rb

Overview

OAuth2 related methods to access API resources

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api, opts) ⇒ OAuth

Instantiate a new Gini::Api::OAuth object and acquire token(s)

Parameters:

  • api (Gini::Api::Client)

    Instance of Gini::Api::Client that contains all required params

  • opts (Hash)

    Your authorization credentials

Options Hash (opts):

  • auth_code (String)

    OAuth authorization code. Will be exchanged for a token

  • username (String)

    API username

  • password (String)

    API password


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/gini-api/oauth.rb', line 20

def initialize(api, opts)
  # Initialize client. max_redirect is required as oauth2 will otherwise redirect to location from header (localhost)
  # https://github.com/intridea/oauth2/blob/master/lib/oauth2/client.rb#L100
  # Our code is encoded in the URL and has to be parsed from there.
  client = OAuth2::Client.new(
    api.client_id,
    api.client_secret,
    site: api.oauth_site,
    token_url: '/oauth/token',
    max_redirects: 0,
    raise_errors: true,
    connection_opts: { headers: { user_agent: api.user_agent } }
  )

  # Verify opts. Prefered authorization methis is auth_code. If no auth_code is present a login from
  # "Resource Owner Password Credentials Grant" flow.
  # @token is a Oauth2::AccessToken object. Accesstoken is @token.token
  @token =
    if opts.key?(:auth_code) && !opts[:auth_code].empty?      # Exchange code for a token

      exchange_code_for_token(api, client, opts[:auth_code])
    else
      #
      (
        client,
        opts[:username],
        opts[:password],
      )
    end

  # Override OAuth2::AccessToken#refresh! to update self instead of returnign a new object
  # Inspired by https://github.com/intridea/oauth2/issues/116#issuecomment-8097675
  #
  # @param [Hash] opts Refresh opts passed to original refresh! method
  #
  # @return [OAuth2::AccessToken] Updated access token object
  #
  def @token.refresh!(opts = {})
    new_token = super
    (new_token.instance_variables - %w[@refresh_token]).each do |name|
      instance_variable_set(name, new_token.instance_variable_get(name))
    end
    self
  end

  # Override OAuth2::AccessToken#request to refresh token when less then 60 seconds left
  #
  # @param [Symbol] verb the HTTP request method
  # @param [String] path the HTTP URL path of the request
  # @param [Hash] opts the options to make the request with
  #
  def @token.request(verb, path, opts = {}, &block)
    refresh! if refresh_token && (expires_at < Time.now.to_i + 60)
    super
  end
end

Instance Attribute Details

#tokenObject (readonly)

Returns the value of attribute token


10
11
12
# File 'lib/gini-api/oauth.rb', line 10

def token
  @token
end

Class Method Details

.refresh!(opts = {}) ⇒ OAuth2::AccessToken

Override OAuth2::AccessToken#refresh! to update self instead of returnign a new object Inspired by github.com/intridea/oauth2/issues/116#issuecomment-8097675

Parameters:

  • opts (Hash) (defaults to: {})

    Refresh opts passed to original refresh! method

Returns:

  • (OAuth2::AccessToken)

    Updated access token object


57
58
59
60
61
62
63
# File 'lib/gini-api/oauth.rb', line 57

def @token.refresh!(opts = {})
  new_token = super
  (new_token.instance_variables - %w[@refresh_token]).each do |name|
    instance_variable_set(name, new_token.instance_variable_get(name))
  end
  self
end

.request(verb, path, opts = {}, &block) ⇒ Object

Override OAuth2::AccessToken#request to refresh token when less then 60 seconds left

Parameters:

  • verb (Symbol)

    the HTTP request method

  • path (String)

    the HTTP URL path of the request

  • opts (Hash) (defaults to: {})

    the options to make the request with


71
72
73
74
# File 'lib/gini-api/oauth.rb', line 71

def @token.request(verb, path, opts = {}, &block)
  refresh! if refresh_token && (expires_at < Time.now.to_i + 60)
  super
end

Instance Method Details

#destroyObject

Destroy token


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/gini-api/oauth.rb', line 79

def destroy
  return "Not implemented yet. Come back later!"

#   @token.refresh_token && @token.refresh!
#   response = token.delete("/accessToken/#{@token.token}")
#   unless response.status == 204
#     fail_with_oauth_error(
#       "Failed to destroy token /accessToken/#{@token.token} "\
#       "(code=#{response.status})",
#       response
#     )
#   end
# rescue OAuth2::Error => e
#   fail_with_oauth_error(
#     "Failed to destroy token (code=#{e.response.status})",
#     e.response
#   )
end