Class: Mumukit::Auth::Token
- Inherits:
-
Object
- Object
- Mumukit::Auth::Token
- Defined in:
- lib/mumukit/auth/token.rb
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#jwt ⇒ Object
readonly
Returns the value of attribute jwt.
Class Method Summary collapse
- .build(uid, client = Mumukit::Auth::Client.new, expiration: nil, organization: nil, subject_id: nil, subject_type: nil, metadata: {}) ⇒ Object
- .decode(encoded, client = Mumukit::Auth::Client.new) ⇒ Object
- .decode_header(header, client = Mumukit::Auth::Client.new) ⇒ Object
- .dump(decoded) ⇒ Object
- .extract_from_header(header) ⇒ Object
- .load(encoded) ⇒ Object
Instance Method Summary collapse
- #encode ⇒ Object
- #encode_header ⇒ Object
- #expiration ⇒ Object
-
#initialize(jwt = {}, client = Mumukit::Auth::Client.new) ⇒ Token
constructor
A new instance of Token.
- #metadata ⇒ Object
- #organization ⇒ Object
- #subject_id ⇒ Object
- #subject_type ⇒ Object
- #uid ⇒ Object
- #verify_client! ⇒ Object
Constructor Details
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
3 4 5 |
# File 'lib/mumukit/auth/token.rb', line 3 def client @client end |
#jwt ⇒ Object (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
#encode ⇒ Object
38 39 40 |
# File 'lib/mumukit/auth/token.rb', line 38 def encode client.encode jwt end |
#encode_header ⇒ Object
42 43 44 |
# File 'lib/mumukit/auth/token.rb', line 42 def encode_header 'Bearer ' + encode end |
#expiration ⇒ Object
22 23 24 |
# File 'lib/mumukit/auth/token.rb', line 22 def expiration @expiration ||= Time.at jwt['exp'] end |
#metadata ⇒ Object
10 11 12 |
# File 'lib/mumukit/auth/token.rb', line 10 def @metadata ||= jwt['metadata'] || {} end |
#organization ⇒ Object
18 19 20 |
# File 'lib/mumukit/auth/token.rb', line 18 def organization @organization ||= jwt['org'] end |
#subject_id ⇒ Object
26 27 28 |
# File 'lib/mumukit/auth/token.rb', line 26 def subject_id @subject_id ||= jwt['sbid'] end |
#subject_type ⇒ Object
30 31 32 |
# File 'lib/mumukit/auth/token.rb', line 30 def subject_type @subject_type ||= jwt['sbt'] end |
#uid ⇒ Object
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 |