Class: Gitlab::ConanToken

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/conan_token.rb

Constant Summary collapse

HMAC_KEY =
'gitlab-conan-packages'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(access_token_id:, user_id:) ⇒ ConanToken

Returns a new instance of ConanToken.


45
46
47
48
# File 'lib/gitlab/conan_token.rb', line 45

def initialize(access_token_id:, user_id:)
  @access_token_id = access_token_id
  @user_id = user_id
end

Instance Attribute Details

#access_token_idObject (readonly)

Returns the value of attribute access_token_id


12
13
14
# File 'lib/gitlab/conan_token.rb', line 12

def access_token_id
  @access_token_id
end

#user_idObject (readonly)

Returns the value of attribute user_id


12
13
14
# File 'lib/gitlab/conan_token.rb', line 12

def user_id
  @user_id
end

Class Method Details

.decode(jwt) ⇒ Object


27
28
29
30
31
32
33
34
# File 'lib/gitlab/conan_token.rb', line 27

def decode(jwt)
  payload = JSONWebToken::HMACToken.decode(jwt, secret).first

  new(access_token_id: payload['access_token'], user_id: payload['user_id'])
rescue JWT::DecodeError, JWT::ExpiredSignature, JWT::ImmatureSignature
  # we return on expired and errored tokens because the Conan client
  # will request a new token automatically.
end

.from_deploy_token(deploy_token) ⇒ Object


23
24
25
# File 'lib/gitlab/conan_token.rb', line 23

def from_deploy_token(deploy_token)
  new(access_token_id: deploy_token.token, user_id: deploy_token.username)
end

.from_job(job) ⇒ Object


19
20
21
# File 'lib/gitlab/conan_token.rb', line 19

def from_job(job)
  new(access_token_id: job.token, user_id: job.user.id)
end

.from_personal_access_token(access_token) ⇒ Object


15
16
17
# File 'lib/gitlab/conan_token.rb', line 15

def from_personal_access_token(access_token)
  new(access_token_id: access_token.id, user_id: access_token.user_id)
end

.secretObject


36
37
38
39
40
41
42
# File 'lib/gitlab/conan_token.rb', line 36

def secret
  OpenSSL::HMAC.hexdigest(
    OpenSSL::Digest::SHA256.new,
    ::Settings.attr_encrypted_db_key_base,
    HMAC_KEY
  )
end

Instance Method Details

#to_jwtObject


50
51
52
# File 'lib/gitlab/conan_token.rb', line 50

def to_jwt
  hmac_token.encoded
end