Class: Wire4Auth::OAuthWire4

Inherits:
Object
  • Object
show all
Defined in:
lib/wire4_auth/auth/oauth_wire4.rb

Constant Summary collapse

MAX_APP_USER_SIZE_CACHED =
100

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client_id, client_secret, environment) ⇒ OAuthWire4

Returns a new instance of OAuthWire4.



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/wire4_auth/auth/oauth_wire4.rb', line 38

def initialize(client_id, client_secret, environment)

  raise 'Not EnvironmentEnum class instance' unless environment.is_a? Wire4Auth::EnvironmentEnum

  @client_id = client_id
  @client_secret = client_secret
  @environment = environment

  @token_cached_app = Wire4Auth::CachedToken.new(nil, nil, nil)
  @tokens_cached_app_user = {}
end

Instance Attribute Details

#client_idObject (readonly)

accessor get method



30
31
32
# File 'lib/wire4_auth/auth/oauth_wire4.rb', line 30

def client_id
  @client_id
end

#client_secretObject (readonly)

accessor get method



33
34
35
# File 'lib/wire4_auth/auth/oauth_wire4.rb', line 33

def client_secret
  @client_secret
end

#environmentObject (readonly)

accessor get method



36
37
38
# File 'lib/wire4_auth/auth/oauth_wire4.rb', line 36

def environment
  @environment
end

Instance Method Details

#config_default_api_clientObject



145
146
147
148
149
150
151
# File 'lib/wire4_auth/auth/oauth_wire4.rb', line 145

def config_default_api_client
  # Setup authorization
  Wire4Client.configure do |config|
    # Configure OAuth2 access token for authorization
    config.host = @environment.service_url
  end
end

#obtain_access_token_app(scope = "general") ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/wire4_auth/auth/oauth_wire4.rb', line 50

def obtain_access_token_app(scope = "general")

  if !@token_cached_app.access_token.nil? and !@token_cached_app.access_token.params.nil? and
      !@token_cached_app.access_token.params['scope'].nil? and
      @token_cached_app.access_token.params['scope'].include? scope and
      !@token_cached_app.access_token.expires_at.nil? and @token_cached_app.access_token.expires_at.is_a? Integer and
      is_expire(@token_cached_app.access_token.expires_at) and !@token_cached_app.access_token.token.nil?

    return format_to_header(@token_cached_app.access_token.token)
  end

  begin
    client = OAuth2::Client.new(@client_id, @client_secret, :token_url => @environment.token_url)
    access_token = client.get_token({:grant_type => "client_credentials", :scope => scope})
    @token_cached_app.access_token = access_token

    return format_to_header(access_token.token)
  rescue OAuth2::Error => e
    raise Wire4Client::ApiError.new(:code => e.code,
                       :message => e.description)
  end
end

#obtain_access_token_app_user(user_key, secret_key, scope = "spei_admin") ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/wire4_auth/auth/oauth_wire4.rb', line 73

def obtain_access_token_app_user(user_key, secret_key, scope = "spei_admin")

  key_search = user_key + scope
  token_cached = @tokens_cached_app_user[key_search]
  if !token_cached.nil? and !token_cached.access_token.nil? and !token_cached.access_token.params.nil? and
      !token_cached.access_token.params['scope'].nil? and token_cached.access_token.params['scope'].include? scope and
      !token_cached.access_token.expires_at.nil? and token_cached.access_token.expires_at.is_a? Integer and
      is_expire(token_cached.access_token.expires_at) and !token_cached.access_token.token.nil?

    return format_to_header(token_cached.access_token.token)
  end

  begin
    client = OAuth2::Client.new(@client_id, @client_secret, :token_url => @environment.token_url)
    access_token = client.get_token({ :grant_type => "password", :scope => scope,
                                              :username => user_key, :password => secret_key })

    if @tokens_cached_app_user.length + 1 > MAX_APP_USER_SIZE_CACHED
      @tokens_cached_app_user.each_key do |key|
        @tokens_cached_app_user.delete(key)
        break
      end
    end

    @tokens_cached_app_user[key_search] = Wire4Auth::CachedToken.new(user_key, secret_key, access_token)

    return format_to_header(access_token.token)
  rescue OAuth2::Error => e
    raise Wire4Client::ApiError.new(:code => e.code,
                       :message => e.description)
  end
end

#regenerate_access_token_app(scope = "general") ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/wire4_auth/auth/oauth_wire4.rb', line 106

def regenerate_access_token_app(scope = "general")

  begin
    client = OAuth2::Client.new(@client_id, @client_secret, :token_url => @environment.token_url)
    access_token = client.get_token({:grant_type => "client_credentials", :scope => scope})
    @token_cached_app.access_token = access_token

    return format_to_header(access_token.token)
  rescue OAuth2::Error => e
    raise Wire4Client::ApiError.new(:code => e.code,
                                    :message => e.description)
  end
end

#regenerate_access_token_app_user(user_key, secret_key, scope = "spei_admin") ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/wire4_auth/auth/oauth_wire4.rb', line 120

def regenerate_access_token_app_user(user_key, secret_key, scope = "spei_admin")

  begin
    client = OAuth2::Client.new(@client_id, @client_secret, :token_url => @environment.token_url)
    access_token = client.get_token({ :grant_type => "password", :scope => scope,
                                      :username => user_key, :password => secret_key })

    key_search = user_key + scope
    token_cached = @tokens_cached_app_user[key_search]
    if token_cached.nil? and @tokens_cached_app_user.length + 1 > MAX_APP_USER_SIZE_CACHED
      @tokens_cached_app_user.each_key do |key|
        @tokens_cached_app_user.delete(key)
        break
      end
    end

    @tokens_cached_app_user[key_search] = Wire4Auth::CachedToken.new(user_key, secret_key, access_token)

    return format_to_header(access_token.token)
  rescue OAuth2::Error => e
    raise Wire4Client::ApiError.new(:code => e.code,
                                    :message => e.description)
  end
end