Sha256: 26a7d491391a1bbbb68a447a0cffec5f098f587e58be4995609772f57a7ec19a

Contents?: true

Size: 1.76 KB

Versions: 2

Compression:

Stored size: 1.76 KB

Contents

# frozen_string_literal: true

module Warden
  module JWTAuth
    # Helper functions to deal with user info present in a decode payload
    module PayloadUserHelper
      # Returns user encoded in given payload
      #
      # @param payload [Hash] JWT payload
      # @return [Interfaces::User] an user, whatever it is
      def self.find_user(payload)
        config = JWTAuth.config
        scope = payload['scp'].to_sym
        user_repo = config.mappings[scope]
        user_repo.find_for_jwt_authentication(payload['sub'])
      end

      # Returns whether given scope matches with the one encoded in the payload
      # @param payload [Hash] JWT payload
      # @return [Boolean]
      def self.scope_matches?(payload, scope)
        payload['scp'] == scope.to_s
      end

      # Returns whether given aud matches with the one encoded in the payload
      # @param payload [Hash] JWT payload
      # @return [Boolean]
      def self.aud_matches?(payload, aud)
        payload['aud'] == aud
      end

      # Returns whether given issuer matches with the one encoded in the payload
      # @param payload [Hash] JWT payload
      # @param issuer [String] The issuer to match
      # @return [Boolean]
      def self.issuer_matches?(payload, issuer)
        payload['iss'] == issuer.to_s
      end

      # Returns the payload to encode for a given user in a scope
      #
      # @param user [Interfaces::User] an user, whatever it is
      # @param scope [Symbol] A Warden scope
      # @return [Hash] payload to encode
      def self.payload_for_user(user, scope)
        sub = user.jwt_subject
        payload = { 'sub' => String(sub), 'scp' => scope.to_s }
        return payload unless user.respond_to?(:jwt_payload)

        user.jwt_payload.merge(payload)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
warden-jwt_auth-0.10.0 lib/warden/jwt_auth/payload_user_helper.rb
warden-jwt_auth-0.9.0 lib/warden/jwt_auth/payload_user_helper.rb