Class: JiraConnect::PublicKey

Inherits:
Object
  • Object
show all
Defined in:
app/models/jira_connect/public_key.rb

Constant Summary collapse

REDIS_EXPIRY_TIME =

Public keys are created with JWT tokens via JiraConnect::CreateAsymmetricJwtService They need to be available for third party applications to verify the token. This should happen right after the application received the token so public keys only need to exist for a few minutes.

5.minutes.to_i.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key:, uuid:) ⇒ PublicKey

Returns a new instance of PublicKey.



27
28
29
30
31
32
33
34
# File 'app/models/jira_connect/public_key.rb', line 27

def initialize(key:, uuid:)
  key = OpenSSL::PKey.read(key) unless key.is_a?(OpenSSL::PKey::RSA)

  @key = key.to_s
  @uuid = uuid
rescue OpenSSL::PKey::PKeyError
  raise ArgumentError, 'Invalid public key'
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



11
12
13
# File 'app/models/jira_connect/public_key.rb', line 11

def key
  @key
end

#uuidObject (readonly)

Returns the value of attribute uuid.



11
12
13
# File 'app/models/jira_connect/public_key.rb', line 11

def uuid
  @uuid
end

Class Method Details

.create!(key:) ⇒ Object



13
14
15
# File 'app/models/jira_connect/public_key.rb', line 13

def self.create!(key:)
  new(key: key, uuid: Gitlab::UUID.v5(SecureRandom.hex)).save!
end

.find(uuid) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'app/models/jira_connect/public_key.rb', line 17

def self.find(uuid)
  Gitlab::Redis::SharedState.with do |redis|
    key = redis.get(redis_key(uuid))

    raise ActiveRecord::RecordNotFound if key.nil?

    new(key: key, uuid: uuid)
  end
end

.redis_key(uuid) ⇒ Object



44
45
46
# File 'app/models/jira_connect/public_key.rb', line 44

def self.redis_key(uuid)
  "JiraConnect:public_key:uuid=#{uuid}"
end

Instance Method Details

#save!Object



36
37
38
39
40
41
42
# File 'app/models/jira_connect/public_key.rb', line 36

def save!
  Gitlab::Redis::SharedState.with do |redis|
    redis.set(self.class.redis_key(uuid), key, ex: REDIS_EXPIRY_TIME)
  end

  self
end