Class: Whoosh::Auth::Jwt
- Inherits:
-
Object
- Object
- Whoosh::Auth::Jwt
- Defined in:
- lib/whoosh/auth/jwt.rb
Instance Method Summary collapse
- #authenticate(request) ⇒ Object
- #generate(sub:, **claims) ⇒ Object
-
#initialize(secret:, algorithm: :hs256, expiry: 3600) ⇒ Jwt
constructor
A new instance of Jwt.
Constructor Details
#initialize(secret:, algorithm: :hs256, expiry: 3600) ⇒ Jwt
Returns a new instance of Jwt.
10 11 12 13 14 |
# File 'lib/whoosh/auth/jwt.rb', line 10 def initialize(secret:, algorithm: :hs256, expiry: 3600) @secret = secret @algorithm = algorithm @expiry = expiry end |
Instance Method Details
#authenticate(request) ⇒ Object
28 29 30 31 32 33 |
# File 'lib/whoosh/auth/jwt.rb', line 28 def authenticate(request) auth_header = request.headers["Authorization"] raise Errors::UnauthorizedError, "Missing authorization header" unless auth_header token = auth_header.sub(/\ABearer\s+/i, "") decode(token) end |
#generate(sub:, **claims) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/whoosh/auth/jwt.rb', line 16 def generate(sub:, **claims) header = { alg: "HS256", typ: "JWT" } now = Time.now.to_i payload = { sub: sub, iat: now, exp: now + @expiry }.merge(claims) header_b64 = base64url_encode(JSON.generate(header)) payload_b64 = base64url_encode(JSON.generate(payload)) signature = sign("#{header_b64}.#{payload_b64}") "#{header_b64}.#{payload_b64}.#{signature}" end |