Module: MSIDP::Endpoint

Defined in:
lib/msidp/endpoint.rb,
lib/msidp/endpoint/version.rb

Overview

Utility methods for Microsoft identity platform endpoints.

Constant Summary collapse

VERSION =
'0.2.0'

Instance Method Summary collapse

Instance Method Details

#authorize(uri, params) ⇒ Net::HTTPResponse

Call the authorize endpoint and returns the response.

Parameters:

  • uri (URI::Generic)

    the endpoint URI.

  • params (Hash)

    parameters to the endpoint.

Returns:

  • (Net::HTTPResponse)

    the endpoint response.



40
41
42
# File 'lib/msidp/endpoint.rb', line 40

def authorize(uri, params)
  get(uri, params)
end

#authorize_uri(tenant) ⇒ Object

Returns the authorize endpoint URI object.

Parameters:

  • tenant (String)

    a directory tenant in GUID or domain-name format.



14
15
16
# File 'lib/msidp/endpoint.rb', line 14

def authorize_uri(tenant)
  uri(tenant, 'authorize')
end

#get(uri, params, headers = nil) ⇒ Net::HTTPResponse

Get with parameters from an endpoint and returns the response.

Parameters:

  • uri (URI::Generic)

    the endpoint URI.

  • params (Hash)

    parameters to the endpoint.

  • headers (Hash) (defaults to: nil)

    additional headers.

Returns:

  • (Net::HTTPResponse)

    the endpoint response.



62
63
64
65
66
67
# 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) ⇒ Net::HTTPResponse

Post parameters to an endpoint and returns the response.

Parameters:

  • uri (URI::Generic)

    the endpoint URI.

  • params (Hash)

    parameters to the endpoint.

  • headers (Hash) (defaults to: nil)

    additional headers.

Returns:

  • (Net::HTTPResponse)

    the endpoint response.



75
76
77
78
79
# 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 = {}) ⇒ AccessToken

Call the token endpoint and returns an issued access token.

Parameters:

  • uri (URI::Generic)

    the endpoint URI.

  • params (Hash)

    parameters to the endpoint.

  • supplement (Hash) (defaults to: {})

    supplemental attributres for the access token.

Returns:



50
51
52
53
54
# 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) ⇒ Object

Returns the token endpoint URI object.

Parameters:

  • tenant (String)

    a directory tenant in GUID or domain-name format.



21
22
23
# File 'lib/msidp/endpoint.rb', line 21

def token_uri(tenant)
  uri(tenant, 'token')
end

#uri(tenant, endpoint) ⇒ Object

Returns an endpoint URI object.

Parameters:

  • tenant (String)

    a directory tenant in GUID or domain-name format.

  • endpoint (String)

    an endpoint.



29
30
31
32
33
# 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) ⇒ Object

Validate a HTTP response supposed to have the JSON body.

Raises:



82
83
84
85
86
87
# 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