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



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/mumukit/auth/token.rb', line 71

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



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

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



62
63
64
# File 'lib/mumukit/auth/token.rb', line 62

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

.dump(decoded) ⇒ Object



93
94
95
# File 'lib/mumukit/auth/token.rb', line 93

def self.dump(decoded)
  decoded.encode
end

.encode(uid, metadata, client = Mumukit::Auth::Client.new) ⇒ Object



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

def self.encode(uid, , client = Mumukit::Auth::Client.new)
  warn "Deprecated: please use build and then encode"
  build(uid, client, metadata: ).encode
end

.encode_header(uid, metadata) ⇒ Object



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

def self.encode_header(uid, )
  warn "Deprecated: please use build and then encode_header"
  'Bearer ' + build(uid, metadata: ).encode_header
end

.extract_from_header(header) ⇒ Object



66
67
68
69
# File 'lib/mumukit/auth/token.rb', line 66

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

.load(encoded) ⇒ Object



87
88
89
90
91
# File 'lib/mumukit/auth/token.rb', line 87

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