Module: Authify::API::Helpers::JWTEncryption
- Includes:
- Core::Helpers::JWTSSL
- Defined in:
- lib/authify/api/helpers/jwt_encryption.rb
Overview
Helper methods for working with JWT encryption
Instance Method Summary collapse
- #jwt_payload(user, custom_data) ⇒ Object
- #jwt_token(user: nil, custom_data: {}) ⇒ Object
- #simple_orgs_by_user(user) ⇒ Object
- #with_jwt(req, scope) ⇒ Object
Instance Method Details
#jwt_payload(user, custom_data) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/authify/api/helpers/jwt_encryption.rb', line 13 def jwt_payload(user, custom_data) data = { exp: Time.now.to_i + 60 * CONFIG[:jwt][:expiration].to_i, iat: Time.now.to_i, iss: CONFIG[:jwt][:issuer], scopes: Core::Constants::JWTSCOPES.dup.tap do |scopes| scopes << :admin_access if user.admin? end, user: { username: user.email, uid: user.id, organizations: simple_orgs_by_user(user) } } data[:custom] = custom_data if custom_data && !custom_data.empty? data end |
#jwt_token(user: nil, custom_data: {}) ⇒ Object
8 9 10 11 |
# File 'lib/authify/api/helpers/jwt_encryption.rb', line 8 def jwt_token(user: nil, custom_data: {}) user ||= current_user JWT.encode jwt_payload(user, custom_data), private_key, CONFIG[:jwt][:algorithm] end |
#simple_orgs_by_user(user) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/authify/api/helpers/jwt_encryption.rb', line 31 def simple_orgs_by_user(user) user.organizations.map do |o| { name: o.name, oid: o.id, admin: o.admins.include?(user), memberships: o.groups.select { |g| g.users.include?(user) }.map do |g| { name: g.name, gid: g.id } end } end end |
#with_jwt(req, scope) ⇒ Object
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/authify/api/helpers/jwt_encryption.rb', line 44 def with_jwt(req, scope) scopes, user = req.env.values_at :scopes, :user set_current_user Models::User.from_username(user['username']) if scopes.include?(scope) && current_user yield req else halt 403 end end |