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

Instance Method Details

#jwt_payload(user) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/authify/api/helpers/jwt_encryption.rb', line 13

def jwt_payload(user)
  {
    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)
    }
  }
end

#jwt_token(user = nil) ⇒ Object



8
9
10
11
# File 'lib/authify/api/helpers/jwt_encryption.rb', line 8

def jwt_token(user = nil)
  user ||= current_user
  JWT.encode jwt_payload(user), private_key, CONFIG[:jwt][:algorithm]
end

#simple_orgs_by_user(user) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/authify/api/helpers/jwt_encryption.rb', line 29

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



42
43
44
45
46
47
48
49
50
51
# File 'lib/authify/api/helpers/jwt_encryption.rb', line 42

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