Class: Gitlab::LfsToken

Inherits:
Object
  • Object
show all
Includes:
LfsTokenHelper
Defined in:
lib/gitlab/lfs_token.rb

Defined Under Namespace

Modules: LfsTokenHelper Classes: HMACToken

Constant Summary collapse

DEFAULT_EXPIRE_TIME =

Default value 2 hours

7200

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from LfsTokenHelper

#actor_name, #user?

Constructor Details

#initialize(actor) ⇒ LfsToken

Returns a new instance of LfsToken.


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

def initialize(actor)
  @actor =
    case actor
    when DeployKey, User
      actor
    when Key
      actor.user
    else
      raise 'Bad Actor'
    end
end

Instance Attribute Details

#actorObject

Returns the value of attribute actor.


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

def actor
  @actor
end

Instance Method Details

#authentication_payload(repository_http_path) ⇒ Object


58
59
60
61
62
63
64
65
# File 'lib/gitlab/lfs_token.rb', line 58

def authentication_payload(repository_http_path)
  {
    username: actor_name,
    lfs_token: token,
    repository_http_path: repository_http_path,
    expires_in: DEFAULT_EXPIRE_TIME
  }
end

#basic_encodingObject


67
68
69
# File 'lib/gitlab/lfs_token.rb', line 67

def basic_encoding
  ActionController::HttpAuthentication::Basic.encode_credentials(actor_name, token)
end

#deploy_key_pushable?(project) ⇒ Boolean

Returns:

  • (Boolean)

44
45
46
# File 'lib/gitlab/lfs_token.rb', line 44

def deploy_key_pushable?(project)
  actor.is_a?(DeployKey) && actor.can_push_to?(project)
end

#tokenObject


33
34
35
# File 'lib/gitlab/lfs_token.rb', line 33

def token
  HMACToken.new(actor).token(DEFAULT_EXPIRE_TIME)
end

#token_valid?(token_to_check) ⇒ Boolean

When the token is an lfs one and the actor is blocked or the password has been changed, the token is no longer valid

Returns:

  • (Boolean)

40
41
42
# File 'lib/gitlab/lfs_token.rb', line 40

def token_valid?(token_to_check)
  HMACToken.new(actor).token_valid?(token_to_check) && valid_user?
end

#typeObject


48
49
50
# File 'lib/gitlab/lfs_token.rb', line 48

def type
  user? ? :lfs_token : :lfs_deploy_token
end

#valid_user?Boolean

Returns:

  • (Boolean)

52
53
54
55
56
# File 'lib/gitlab/lfs_token.rb', line 52

def valid_user?
  return true unless user?

  !actor.blocked? && !actor.password_expired_if_applicable?
end