Class: Gitlab::Auth::Atlassian::TokenRefresher

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/auth/atlassian/token_refresher.rb

Constant Summary collapse

REFRESH_TOKEN_URL =
'https://auth.atlassian.com/oauth/token'
MIN_TIME_ALLOWED_TILL_EXPIRE =
5.minutes
AtlassianTokenRefreshError =
Class.new(StandardError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(identity) ⇒ TokenRefresher

Returns a new instance of TokenRefresher.



13
14
15
# File 'lib/gitlab/auth/atlassian/token_refresher.rb', line 13

def initialize(identity)
  @identity = identity
end

Instance Attribute Details

#identityObject (readonly)

Returns the value of attribute identity.



7
8
9
# File 'lib/gitlab/auth/atlassian/token_refresher.rb', line 7

def identity
  @identity
end

Instance Method Details

#needs_refresh?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/gitlab/auth/atlassian/token_refresher.rb', line 17

def needs_refresh?
  identity.expires_at < MIN_TIME_ALLOWED_TILL_EXPIRE.from_now
end

#refresh!Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/gitlab/auth/atlassian/token_refresher.rb', line 21

def refresh!
  response = Gitlab::HTTP.post(REFRESH_TOKEN_URL, body: payload.to_json, headers: headers)
  raise AtlassianTokenRefreshError, response["error"] unless response.success?

  identity.update!(
    expires_at: Time.zone.now + response["expires_in"].seconds,
    refresh_token: response["refresh_token"],
    token: response["access_token"]
  )
end

#refresh_if_needed!Object



32
33
34
# File 'lib/gitlab/auth/atlassian/token_refresher.rb', line 32

def refresh_if_needed!
  refresh! if needs_refresh?
end