class Mumukit::Auth::Token

Attributes

client[R]
jwt[R]

Public Class Methods

decode(encoded, client = Mumukit::Auth::Client.new) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/mumukit/auth/token.rb, line 30
def self.encode(uid, metadata, client = Mumukit::Auth::Client.new)
  new({aud: client.id, metadata: metadata, uid: uid}, client).encode
end
encode_header(uid, metadata) click to toggle source
# File lib/mumukit/auth/token.rb, line 40
def self.encode_header(uid, metadata)
  'Bearer ' + encode(uid, metadata)
end
extract_from_header(header) click to toggle source
# 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) click to toggle source
# File lib/mumukit/auth/token.rb, line 26
def self.from_rack_env(env)
  new(env.dig('omniauth.auth', 'extra', 'raw_info') || {})
end
new(jwt, client) click to toggle source
# File lib/mumukit/auth/token.rb, line 5
def initialize(jwt, client)
  @jwt = jwt
  @client = client
end

Public Instance Methods

encode() click to toggle source
# File lib/mumukit/auth/token.rb, line 22
def encode
  client.encode jwt
end
metadata() click to toggle source
# File lib/mumukit/auth/token.rb, line 10
def metadata
  @metadata ||= jwt['metadata'] || {}
end
uid() click to toggle source
# File lib/mumukit/auth/token.rb, line 14
def uid
  @uid ||= jwt['uid'] || jwt['email'] || jwt['sub']
end
verify_client!() click to toggle source
# File lib/mumukit/auth/token.rb, line 18
def verify_client!
  raise Mumukit::Auth::InvalidTokenError.new('aud mismatch') if client.id != jwt['aud']
end