Class: Userlist::Token
- Inherits:
-
Object
- Object
- Userlist::Token
- Defined in:
- lib/userlist/token.rb
Class Method Summary collapse
Instance Method Summary collapse
- #encoded_header ⇒ Object
- #encoded_header_and_payload ⇒ Object
- #encoded_payload ⇒ Object
-
#initialize(payload:, header:, key:, algorithm: 'HS256') ⇒ Token
constructor
A new instance of Token.
- #signature ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(payload:, header:, key:, algorithm: 'HS256') ⇒ Token
Returns a new instance of Token.
28 29 30 31 32 33 |
# File 'lib/userlist/token.rb', line 28 def initialize(payload:, header:, key:, algorithm: 'HS256') @payload = payload @header = header @algorithm = algorithm @key = key end |
Class Method Details
.generate(user, configuration = {}) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/userlist/token.rb', line 3 def self.generate(user, configuration = {}) config = Userlist.config.merge(configuration) raise Userlist::ConfigurationError, :push_key unless config.push_key raise Userlist::ConfigurationError, :push_id unless config.push_id raise Userlist::ArgumentError, 'Missing required user or identifier' unless user user = Userlist::Push::User.from_payload(user, config) now = Time.now.utc.to_i header = { kid: config.push_id, alg: 'HS256' } payload = { sub: user.identifier, exp: now + config.token_lifetime, iat: now } new(payload: payload, header: header, key: config.push_key).to_s end |
Instance Method Details
#encoded_header ⇒ Object
35 36 37 |
# File 'lib/userlist/token.rb', line 35 def encoded_header encode(header) end |
#encoded_header_and_payload ⇒ Object
43 44 45 |
# File 'lib/userlist/token.rb', line 43 def encoded_header_and_payload "#{encoded_header}.#{encoded_payload}" end |
#encoded_payload ⇒ Object
39 40 41 |
# File 'lib/userlist/token.rb', line 39 def encoded_payload encode(payload) end |
#signature ⇒ Object
47 48 49 50 51 52 |
# File 'lib/userlist/token.rb', line 47 def signature digest = OpenSSL::Digest.new(algorithm.sub('HS', 'SHA')) signature = OpenSSL::HMAC.digest(digest, key, encoded_header_and_payload) encode(signature) end |
#to_s ⇒ Object
54 55 56 |
# File 'lib/userlist/token.rb', line 54 def to_s "#{encoded_header_and_payload}.#{signature}" end |