Sha256: bbfbcfac62a4c85189134fdbb72c0b35bdbee1edff90d6ba18212f0d9e577220

Contents?: true

Size: 911 Bytes

Versions: 6

Compression:

Stored size: 911 Bytes

Contents

module DMAO
  module WardenJWT
    class User

      attr_reader(:id, :subject_id, :institution_id, :roles)

      def initialize(attributes)

        @id = attributes[:uid] || nil
        @subject_id = attributes[:subject_id] || nil
        @institution_id = attributes[:institution_id] || nil
        @roles = attributes[:roles] || []

      end

      def self.from_jwt_claims jwt_claims

        custom_claims = ENV['JWT_CUSTOM_CLAIMS_ATTRIBUTE']

        uid = jwt_claims[custom_claims]["uid"].nil? ? jwt_claims["sub"] : jwt_claims[custom_claims]["uid"]

        attributes = {
            uid: uid,
            subject_id: jwt_claims["sub"],
            institution_id: jwt_claims[custom_claims]["institution_id"],
            roles: jwt_claims[custom_claims]["roles"]
        }

        new(attributes)

      end

      def has_role? role

        @roles.include? role.to_s

      end

    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
DMAO-WardenJWT-0.6.0 lib/DMAO/WardenJWT/user.rb
DMAO-WardenJWT-0.5.0 lib/DMAO/WardenJWT/user.rb
DMAO-WardenJWT-0.4.0 lib/DMAO/WardenJWT/user.rb
DMAO-WardenJWT-0.3.0 lib/DMAO/WardenJWT/user.rb
DMAO-WardenJWT-0.2.0 lib/DMAO/WardenJWT/user.rb
DMAO-WardenJWT-0.1.0 lib/DMAO/WardenJWT/user.rb