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



168
169
170
171
172
173
174
175
# File 'lib/wire4_auth/auth/oauth_wire4.rb', line 168

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

#obtain_access_token_app(scope = "general") ⇒ Object

noinspection DuplicatedCode



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
76
77
78
79
80
81
# File 'lib/wire4_auth/auth/oauth_wire4.rb', line 51

def obtain_access_token_app(scope = "general")
  key_search = @client_id + 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.expires_at.nil? and
      token_cached.access_token.expires_at.is_a? Integer and is_valid(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 => "client_credentials", :scope => scope})

    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(@client_id, @client_secret, 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

noinspection DuplicatedCode



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/wire4_auth/auth/oauth_wire4.rb', line 84

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.expires_at.nil? and
      token_cached.access_token.expires_at.is_a? Integer and is_valid(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



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

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})

    key_search = @client_id + 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(@client_id, @client_secret, 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

noinspection RubyInstanceMethodNamingConvention



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/wire4_auth/auth/oauth_wire4.rb', line 143

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