Class: SupportOps::GitLab::UserTokens

Inherits:
Base
  • Object
show all
Defined in:
lib/support_ops_gitlab/gitlab/user_tokens.rb

Overview

TODO:

Defines the class UserTokens within the module SupportOps::GitLab.

Author:

  • Jason Colyer

Since:

  • 1.0.0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#activate!, #approve!, attributes, #badges, #ban!, #block!, client, #client=, #commits, configure, #contributors, #create_support_pin!, #deactivate!, define_attributes, #delete!, #diffs, #disable_2fa!, #discussions, #emails, #encoded_path, #events, #find, #find!, #gpg_keys, #hard_delete!, #impersonations, #initialize, #issues, #jobs, #latest_pipeline, #members, #memberships, #merge_requests, #move!, #notes, #paid?, #pats, #pipeline_variables, #pipelines, #preferences, #projects, readonly_attributes, #reject!, #ssh_keys, #status, #store_original_attributes, #subscribe!, #support_pin, to_hash, #tokens, #unban!, #unblock!, #unsubscribe!, #webhooks

Constructor Details

This class inherits a constructor from SupportOps::GitLab::Base

Instance Attribute Details

#activeBoolean

If the token is active or not

Returns:

  • (Boolean)

    the current value of active



26
27
28
# File 'lib/support_ops_gitlab/gitlab/user_tokens.rb', line 26

def active
  @active
end

#created_atString

Timestamp when the token was created

Returns:

  • (String)

    the current value of created_at



26
27
28
# File 'lib/support_ops_gitlab/gitlab/user_tokens.rb', line 26

def created_at
  @created_at
end

#descriptionString

Description of token

Returns:

  • (String)

    the current value of description



26
27
28
# File 'lib/support_ops_gitlab/gitlab/user_tokens.rb', line 26

def description
  @description
end

#expires_atString

Expiration date of the token in ISO format (YYYY-MM-DD)

Returns:

  • (String)

    the current value of expires_at



26
27
28
# File 'lib/support_ops_gitlab/gitlab/user_tokens.rb', line 26

def expires_at
  @expires_at
end

#idInteger

ID of token

Returns:

  • (Integer)

    the current value of id



26
27
28
# File 'lib/support_ops_gitlab/gitlab/user_tokens.rb', line 26

def id
  @id
end

#impersonationBoolean

If the token is an impersonation token

Returns:

  • (Boolean)

    the current value of impersonation



26
27
28
# File 'lib/support_ops_gitlab/gitlab/user_tokens.rb', line 26

def impersonation
  @impersonation
end

#nameString

Name of token

Returns:

  • (String)

    the current value of name



26
27
28
# File 'lib/support_ops_gitlab/gitlab/user_tokens.rb', line 26

def name
  @name
end

#revokedBoolean

If the token is revoked or not

Returns:

  • (Boolean)

    the current value of revoked



26
27
28
# File 'lib/support_ops_gitlab/gitlab/user_tokens.rb', line 26

def revoked
  @revoked
end

#scopesArray

Array of approved scopes

Returns:

  • (Array)

    the current value of scopes



26
27
28
# File 'lib/support_ops_gitlab/gitlab/user_tokens.rb', line 26

def scopes
  @scopes
end

#tokenString

The token itself

Returns:

  • (String)

    the current value of token



26
27
28
# File 'lib/support_ops_gitlab/gitlab/user_tokens.rb', line 26

def token
  @token
end

#user_idInteger

ID of user account

Returns:

  • (Integer)

    the current value of user_id



26
27
28
# File 'lib/support_ops_gitlab/gitlab/user_tokens.rb', line 26

def user_id
  @user_id
end

Class Method Details

.list(key: value) ⇒ Array

List all impersonation tokens for a user

Examples:

require 'support_ops_gitlab'

SupportOps::GitLab::Configuration.configure do |config|
  config.url = 'https://gitlab.example.com/api/v4'
  config.token = 'abc123'
end

user = Readiness::GitLab::Users.current
tokens = SupportOps::GitLab::UserTokens.list_impersonation(state: 'active', user_id: user.id)
pp tokens.count
# => 1
pp tokens.last.expires_at
# => "2025-08-15"

Parameters:

  • state (String optional)

    if defined, returns tokens with the specified statel possible values: active and inactive

  • user_id (Integer required)

    if defined, returns tokens owned by the specified user

Returns:

  • (Array)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/support_ops_gitlab/gitlab/user_tokens.rb', line 265

def self.list_impersonation(**args)
  raise 'You must provide a user_id for this' unless args[:user_id]
  args[:state] = nil unless args[:state]
  params = ''
  params += "state=#{args[:state]}&" unless args[:state].nil?
  tokens = []
  page = 1
  loop do
    response = client.connection.get("users/#{args[:user_id]}/impersonation_tokens?#{params}&page=#{page}&per_page=100")
    body = Oj.load(response.body)
    tokens += body.map { |p| UserTokens.new(p) }
    break if body.count < 100

    page += 1
  end
  tokens
end

.list(key: value) ⇒ Array

List all personal access tokens

Examples:

require 'support_ops_gitlab'

SupportOps::GitLab::Configuration.configure do |config|
  config.url = 'https://gitlab.example.com/api/v4'
  config.token = 'abc123'
end

pats = SupportOps::GitLab::UserTokens.list_paths(revoked: false)
pp pats.count
# => 2
pp pats.last.expires_at
# => "2025-08-15"

Parameters:

  • created_after (String optional)

    If defined, returns tokens created after the specified time (ISO 8601)

  • created_before (String optional)

    If defined, returns tokens created before the specified time (ISO 8601)

  • expires_after (String optional)

    If defined, returns tokens that expire after the specified time (ISO 8601)

  • expires_before (String optional)

    If defined, returns tokens that expire before the specified time (ISO 8601)

  • last_used_after (String optional)

    If defined, returns tokens last used after the specified time (ISO 8601)

  • last_used_before (String optional)

    If defined, returns tokens last used before the specified time (ISO 8601)

  • revoked (Boolean optional)

    If true, only returns revoked tokens

  • search (String optional)

    If defined, returns tokens that include the specified value in the name

  • state (String optional)

    if defined, returns tokens with the specified statel possible values: active and inactive

  • user_id (Integer optional)

    if defined, returns tokens owned by the specified user

Returns:

  • (Array)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/support_ops_gitlab/gitlab/user_tokens.rb', line 201

def self.list_pats(**args)
  args[:created_after] = nil unless args[:created_after]
  args[:created_before] = nil unless args[:created_before]
  args[:expires_after] = nil unless args[:expires_after]
  args[:expires_before] = nil unless args[:expires_before]
  args[:last_used_after] = nil unless args[:last_used_after]
  args[:last_used_before] = nil unless args[:last_used_before]
  args[:revoked] = nil unless args[:revoked]
  args[:search] = nil unless args[:search]
  args[:state] = nil unless args[:state]
  args[:user_id] = nil unless args[:user_id]
  params = ''
  params += "created_after=#{args[:created_after]}&" unless args[:created_after].nil?
  params += "created_before=#{args[:created_before]}&" unless args[:created_before].nil?
  params += "expires_after=#{args[:expires_after]}&" unless args[:expires_after].nil?
  params += "expires_before=#{args[:expires_before]}&" unless args[:expires_before].nil?
  params += "last_used_after=#{args[:last_used_after]}&" unless args[:last_used_after].nil?
  params += "last_used_before=#{args[:last_used_before]}&" unless args[:last_used_before].nil?
  params += "revoked=#{args[:revoked]}&" unless args[:revoked].nil?
  params += "search=#{args[:search]}&" unless args[:search].nil?
  params += "state=#{args[:state]}&" unless args[:state].nil?
  params += "user_id=#{args[:user_id]}&" unless args[:user_id].nil?
  tokens = []
  page = 1
  loop do
    response = client.connection.get("personal_access_tokens?#{params}&page=#{page}&per_page=100")
    body = Oj.load(response.body)
    tokens += body.map { |p| UserTokens.new(p) }
    break if body.count < 100

    page += 1
  end
  tokens
end

Instance Method Details

#revoke!Object

Note:

This is inherited from Base#revoke!

Revokes a token (personal access or impersonation)

Examples:

require 'support_ops_gitlab'

SupportOps::GitLab::Configuration.configure do |config|
  config.token = ENV.fetch('GL_TOKEN')
  config.url = 'https://gitlab.com/api/v4'
end

user = SupportOps::GitLab::Users.get!(123456)
tokens = users.pats
tokens.last.revoke!

Returns:

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



24
# File 'lib/support_ops_gitlab/gitlab/user_tokens.rb', line 24

def revoke!; end

#rotate!Object

Note:

This is inherited from Base#rotate!

Rotates a personal access token (does not work on impersonation tokens)

Examples:

require 'support_ops_gitlab'

SupportOps::GitLab::Configuration.configure do |config|
  config.token = ENV.fetch('GL_TOKEN')
  config.url = 'https://gitlab.com/api/v4'
end

user = SupportOps::GitLab::Users.get!(123456)
tokens = users.pats
new_token = tokens.last.rotate!
pp new_token.token
# => "glpat-def456"

Returns:

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



23
# File 'lib/support_ops_gitlab/gitlab/user_tokens.rb', line 23

def rotate!; end

#save!Object

Note:

This is inherited from Base#save!

Creates a user token (personal access or impersonation)

Examples:

require 'support_ops_gitlab'

SupportOps::GitLab::Configuration.configure do |config|
  config.token = ENV.fetch('GL_TOKEN')
  config.url = 'https://gitlab.com/api/v4'
end

user = SupportOps::GitLab::Users.get!(123456)
new_personal_access_token = SupportOps::GitLab::UserTokens.new
new_personal_access_token.name = 'Jason Test'
new_personal_access_token.expires_at = '2025-05-28'
new_personal_access_token.description = 'Jason Test Desc'
new_personal_access_token.scopes = ['api']
new_personal_access_token.user_id = user.id

new_personal_access_token.save!

pp new_personal_access_token.token
# => "glpat-abc123"
require 'support_ops_gitlab'

SupportOps::GitLab::Configuration.configure do |config|
  config.token = ENV.fetch('GL_TOKEN')
  config.url = 'https://gitlab.com/api/v4'
end

new_personal_access_token = SupportOps::GitLab::UserTokens.new
new_personal_access_token.name = 'Jason Test'
new_personal_access_token.expires_at = '2025-05-28'
new_personal_access_token.description = 'Jason Test Desc'
new_personal_access_token.scopes = ['k8s_proxy']

new_personal_access_token.save!

pp new_personal_access_token.token
# => "glpat-def456"
require 'support_ops_gitlab'

SupportOps::GitLab::Configuration.configure do |config|
  config.token = ENV.fetch('GL_TOKEN')
  config.url = 'https://gitlab.com/api/v4'
end

user = SupportOps::GitLab::Users.get!(123456)
new_impersonation_token = SupportOps::GitLab::UserTokens.new
new_impersonation_token.name = 'Jason Test'
new_impersonation_token.expires_at = '2025-05-28'
new_impersonation_token.description = 'Jason Test Desc'
new_impersonation_token.scopes = ['api']
new_impersonation_token.user_id = user.id
new_impersonation_token.impersonation = true

new_impersonation_token.save!

pp new_impersonation_token.token
# => "glpat-ghi789"

Returns:

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



75
# File 'lib/support_ops_gitlab/gitlab/user_tokens.rb', line 75

def save!; end