Class: Compeon::AccessToken

Inherits:
Object
  • Object
show all
Defined in:
lib/compeon/access_token.rb,
lib/compeon/access_token/version.rb

Defined Under Namespace

Classes: ParseError

Constant Summary collapse

VERSION =
'0.2.0'.freeze

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(role:, user_id:, kind:, client_id:, token:) ⇒ AccessToken

Returns a new instance of AccessToken.



12
13
14
15
16
17
18
# File 'lib/compeon/access_token.rb', line 12

def initialize(role:, user_id:, kind:, client_id:, token:)
  @role = role
  @user_id = user_id
  @kind = kind
  @client_id = client_id
  @token = token
end

Class Attribute Details

.environmentObject



25
26
27
28
29
# File 'lib/compeon/access_token.rb', line 25

def environment
  @environment ||
    ENV['ENVIRONMENT'] ||
    raise("`#{self}.environment` or `ENV['ENVIRONMENT']` must be set")
end

Instance Attribute Details

#client_idObject (readonly)

Returns the value of attribute client_id.



20
21
22
# File 'lib/compeon/access_token.rb', line 20

def client_id
  @client_id
end

#kindObject (readonly)

Returns the value of attribute kind.



20
21
22
# File 'lib/compeon/access_token.rb', line 20

def kind
  @kind
end

#roleObject (readonly)

Returns the value of attribute role.



20
21
22
# File 'lib/compeon/access_token.rb', line 20

def role
  @role
end

#tokenObject (readonly)

Returns the value of attribute token.



20
21
22
# File 'lib/compeon/access_token.rb', line 20

def token
  @token
end

#user_idObject (readonly)

Returns the value of attribute user_id.



20
21
22
# File 'lib/compeon/access_token.rb', line 20

def user_id
  @user_id
end

Class Method Details

.parse(token) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/compeon/access_token.rb', line 31

def parse(token)
  data, _header = JWT.decode(token, public_key, false, algorithm: 'RS256')

  role = data.fetch('role')
  user_id = data.fetch('uid')
  kind = data.fetch('knd')
  client_id = data.fetch('cid')

  new(role: role, user_id: user_id, kind: kind, client_id: client_id, token: token)
rescue JWT::DecodeError
  raise ParseError
end

.public_keyObject



44
45
46
# File 'lib/compeon/access_token.rb', line 44

def public_key
  @public_key ||= OpenSSL::PKey::RSA.new(public_key_string)
end

.public_key_stringObject



53
54
55
56
57
58
# File 'lib/compeon/access_token.rb', line 53

def public_key_string
  @public_key_string ||= begin
    env_subdomain = environment != 'production' ? ".#{environment}" : nil
    URI.parse("https://login#{env_subdomain}.compeon.de/public-key").read
  end
end

.public_key_string=(value) ⇒ Object



48
49
50
51
# File 'lib/compeon/access_token.rb', line 48

def public_key_string=(value)
  @public_key = nil
  @public_key_string = value
end