Class: Mumukit::Auth::Token

Inherits:
Object
  • Object
show all
Defined in:
lib/mumukit/auth/token.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(jwt = {}, client = Mumukit::Auth::Client.new) ⇒ Token

Returns a new instance of Token.



5
6
7
8
# File 'lib/mumukit/auth/token.rb', line 5

def initialize(jwt = {}, client = Mumukit::Auth::Client.new)
  @jwt = jwt
  @client = client
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



3
4
5
# File 'lib/mumukit/auth/token.rb', line 3

def client
  @client
end

#jwtObject (readonly)

Returns the value of attribute jwt.



3
4
5
# File 'lib/mumukit/auth/token.rb', line 3

def jwt
  @jwt
end

Class Method Details

.build(uid, client = Mumukit::Auth::Client.new, expiration: nil, organization: nil, subject_id: nil, subject_type: nil, metadata: {}) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/mumukit/auth/token.rb', line 61

def self.build(uid, client = Mumukit::Auth::Client.new,
               expiration: nil, organization: nil,
               subject_id: nil, subject_type: nil,
               metadata: {})
  new({
      'uid' => uid,
      'aud' => client.id,
      'exp' => expiration&.to_i,
      'org' => organization,
      'metadata' => ,
      'sbid' => subject_id,
      'sbt' => subject_type
    }.compact,
    client)
end

.decode(encoded, client = Mumukit::Auth::Client.new) ⇒ Object



46
47
48
49
50
# File 'lib/mumukit/auth/token.rb', line 46

def self.decode(encoded, client = Mumukit::Auth::Client.new)
  new client.decode(encoded), client
rescue JWT::DecodeError => e
  raise Mumukit::Auth::InvalidTokenError.new(e)
end

.decode_header(header, client = Mumukit::Auth::Client.new) ⇒ Object



52
53
54
# File 'lib/mumukit/auth/token.rb', line 52

def self.decode_header(header, client = Mumukit::Auth::Client.new)
  decode extract_from_header(header), client
end

.dump(decoded) ⇒ Object



83
84
85
# File 'lib/mumukit/auth/token.rb', line 83

def self.dump(decoded)
  decoded.encode
end

.extract_from_header(header) ⇒ Object



56
57
58
59
# File 'lib/mumukit/auth/token.rb', line 56

def self.extract_from_header(header)
  raise Mumukit::Auth::InvalidTokenError.new('missing authorization header') if header.nil?
  header.split(' ').last
end

.load(encoded) ⇒ Object



77
78
79
80
81
# File 'lib/mumukit/auth/token.rb', line 77

def self.load(encoded)
  if encoded.present?
    decode encoded rescue nil
  end
end

Instance Method Details

#encodeObject



38
39
40
# File 'lib/mumukit/auth/token.rb', line 38

def encode
  client.encode jwt
end

#encode_headerObject



42
43
44
# File 'lib/mumukit/auth/token.rb', line 42

def encode_header
  'Bearer ' + encode
end

#expirationObject



22
23
24
# File 'lib/mumukit/auth/token.rb', line 22

def expiration
  @expiration ||= Time.at jwt['exp']
end

#metadataObject



10
11
12
# File 'lib/mumukit/auth/token.rb', line 10

def 
  @metadata ||= jwt['metadata'] || {}
end

#organizationObject



18
19
20
# File 'lib/mumukit/auth/token.rb', line 18

def organization
  @organization ||= jwt['org']
end

#subject_idObject



26
27
28
# File 'lib/mumukit/auth/token.rb', line 26

def subject_id
  @subject_id ||= jwt['sbid']
end

#subject_typeObject



30
31
32
# File 'lib/mumukit/auth/token.rb', line 30

def subject_type
  @subject_type ||= jwt['sbt']
end

#uidObject



14
15
16
# File 'lib/mumukit/auth/token.rb', line 14

def uid
  @uid ||= jwt['uid'] || jwt['email'] || jwt['sub']
end

#verify_client!Object



34
35
36
# File 'lib/mumukit/auth/token.rb', line 34

def verify_client!
  raise Mumukit::Auth::InvalidTokenError.new('aud mismatch') if client.id != jwt['aud']
end