module MSIDP::Endpoint

Utility methods for Microsoft identity platform endpoints.

Constants

VERSION

Public Instance Methods

authorize(uri, params) click to toggle source

Call the authorize endpoint and returns the response.

@param [URI::Generic] uri the endpoint URI. @param [Hash] params parameters to the endpoint. @return [Net::HTTPResponse] the endpoint response.

# File lib/msidp/endpoint.rb, line 40
def authorize(uri, params)
  get(uri, params)
end
authorize_uri(tenant) click to toggle source

Returns the authorize endpoint URI object.

@param [String] tenant a directory tenant in GUID or domain-name format.

# File lib/msidp/endpoint.rb, line 14
def authorize_uri(tenant)
  uri(tenant, 'authorize')
end
get(uri, params, headers = nil) click to toggle source

Get with parameters from an endpoint and returns the response.

@param [URI::Generic] uri the endpoint URI. @param [Hash] params parameters to the endpoint. @param [Hash] headers additional headers. @return [Net::HTTPResponse] the endpoint response.

# File lib/msidp/endpoint.rb, line 62
def get(uri, params, headers = nil)
  req_uri = URI.parse(uri.request_uri)
  req_uri.query = URI.encode_www_form(params) if params
  req = Net::HTTP::Get.new(req_uri.to_s, headers)
  https_request(uri, req)
end
post(uri, params, headers = nil) click to toggle source

Post parameters to an endpoint and returns the response.

@param [URI::Generic] uri the endpoint URI. @param [Hash] params parameters to the endpoint. @param [Hash] headers additional headers. @return [Net::HTTPResponse] the endpoint response.

# File lib/msidp/endpoint.rb, line 75
def post(uri, params, headers = nil)
  req = Net::HTTP::Post.new(uri.request_uri, headers)
  req.form_data = params if params
  https_request(uri, req)
end
token(uri, params, supplement = {}) click to toggle source

Call the token endpoint and returns an issued access token.

@param [URI::Generic] uri the endpoint URI. @param [Hash] params parameters to the endpoint. @param [Hash] supplement supplemental attributres for the access token. @return [AccessToken] the issued access token.

# File lib/msidp/endpoint.rb, line 50
def token(uri, params, supplement = {})
  res = validate_json_response post(uri, params)
  suppl = params.select { |k| k.intern == :scope }.merge(supplement)
  AccessToken.parse(res, suppl)
end
token_uri(tenant) click to toggle source

Returns the token endpoint URI object.

@param [String] tenant a directory tenant in GUID or domain-name format.

# File lib/msidp/endpoint.rb, line 21
def token_uri(tenant)
  uri(tenant, 'token')
end
uri(tenant, endpoint) click to toggle source

Returns an endpoint URI object.

@param [String] tenant a directory tenant in GUID or domain-name format. @param [String] endpoint an endpoint.

# File lib/msidp/endpoint.rb, line 29
def uri(tenant, endpoint)
  URI.parse(
    "https://login.microsoftonline.com/#{tenant}/oauth2/v2.0/#{endpoint}"
  )
end
validate_json_response(res) click to toggle source

Validate a HTTP response supposed to have the JSON body.

# File lib/msidp/endpoint.rb, line 82
def validate_json_response(res)
  raise Error, res unless res.is_a? Net::HTTPSuccess
  raise Error, res unless res.content_type&.start_with? 'application/json'

  res
end

Private Instance Methods

https_request(uri, req) click to toggle source
# File lib/msidp/endpoint.rb, line 91
def https_request(uri, req)
  Net::HTTP.start(uri.host, uri.port,
                  use_ssl: true,
                  min_version: OpenSSL::SSL::TLS1_2_VERSION) do |http|
    res = http.request(req)
    case res
    when Net::HTTPSuccess, Net::HTTPRedirection
      res
    else
      raise Error, res
    end
  end
end