Sha256: c539b2e9b5f9a5ee41101fc21bb964117ad1a7ab0f9288308d6fb4a155afd6a5

Contents?: true

Size: 1.22 KB

Versions: 1

Compression:

Stored size: 1.22 KB

Contents

require 'rest-client'
require 'json'
require 'dmao/jwt/api/base'
require 'dmao/jwt/api/errors/invalid_jwt_issuer'
require 'dmao/jwt/api/errors/unable_to_issue_token'

module DMAO
  module JWT
    module API

      class TokenIssuer

        def initialize issuer_name
          @issuer_name = issuer_name
        end

        def request_token subject, institution_id, roles

          if @issuer_name.nil? || @issuer_name.empty?
            raise DMAO::JWT::API::Errors::InvalidJWTIssuer.new("JWT Issuer not set for token generator")
          end

          custom_claims = {
              institution_id: institution_id,
              roles: roles
          }

          request_attributes = {
              subject: subject,
              custom_claims: custom_claims
          }

          begin
            response = DMAO::JWT::API::Base.api["#{@issuer_name}/tokens"].post request_attributes.to_json
          rescue RestClient::NotFound
            raise DMAO::JWT::API::Errors::InvalidJWTIssuer
          rescue RestClient::UnprocessableEntity, RestClient::InternalServerError
            raise DMAO::JWT::API::Errors::UnableToIssueToken
          end

          JSON.parse(response.body)["token"]

        end

      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dmao-jwt-api-0.1.0 lib/dmao/jwt/api/token_issuer.rb