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
- .decode(encoded, client = Mumukit::Auth::Client.new) ⇒ Object
- .decode_header(header, client = Mumukit::Auth::Client.new) ⇒ Object
- .encode(uid, metadata, client = Mumukit::Auth::Client.new) ⇒ Object
- .encode_header(uid, metadata) ⇒ Object
- .extract_from_header(header) ⇒ Object
- .from_rack_env(env) ⇒ Object
Instance Method Summary collapse
- #encode ⇒ Object
-
#initialize(jwt, client) ⇒ Token
constructor
A new instance of Token.
- #metadata ⇒ Object
- #uid ⇒ Object
- #verify_client! ⇒ Object
Constructor Details
#initialize(jwt, client) ⇒ Token
Returns a new instance of Token.
5 6 7 8 |
# File 'lib/mumukit/auth/token.rb', line 5 def initialize(jwt, client) @jwt = jwt @client = client end |
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
.decode(encoded, client = Mumukit::Auth::Client.new) ⇒ Object
34 35 36 37 38 |
# File 'lib/mumukit/auth/token.rb', line 34 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
44 45 46 |
# File 'lib/mumukit/auth/token.rb', line 44 def self.decode_header(header, client = Mumukit::Auth::Client.new) decode extract_from_header(header), client end |
.encode(uid, metadata, client = Mumukit::Auth::Client.new) ⇒ Object
30 31 32 |
# File 'lib/mumukit/auth/token.rb', line 30 def self.encode(uid, , client = Mumukit::Auth::Client.new) new({aud: client.id, metadata: , uid: uid}, client).encode end |
.encode_header(uid, metadata) ⇒ Object
40 41 42 |
# File 'lib/mumukit/auth/token.rb', line 40 def self.encode_header(uid, ) 'Bearer ' + encode(uid, ) end |
.extract_from_header(header) ⇒ Object
48 49 50 51 |
# File 'lib/mumukit/auth/token.rb', line 48 def self.extract_from_header(header) raise Mumukit::Auth::InvalidTokenError.new('missing authorization header') if header.nil? header.split(' ').last end |
.from_rack_env(env) ⇒ Object
26 27 28 |
# File 'lib/mumukit/auth/token.rb', line 26 def self.from_rack_env(env) new(env.dig('omniauth.auth', 'extra', 'raw_info') || {}) end |
Instance Method Details
#encode ⇒ Object
22 23 24 |
# File 'lib/mumukit/auth/token.rb', line 22 def encode client.encode jwt end |
#metadata ⇒ Object
10 11 12 |
# File 'lib/mumukit/auth/token.rb', line 10 def ||= jwt['metadata'] || {} 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
18 19 20 |
# File 'lib/mumukit/auth/token.rb', line 18 def verify_client! raise Mumukit::Auth::InvalidTokenError.new('aud mismatch') if client.id != jwt['aud'] end |