class AadAuth::Aad

Public Instance Methods

auth!(token) click to toggle source
# File lib/aad_auth.rb, line 8
def auth!(token)
  keys = self.get_jwk_set()
  jwk_loader = ->(options) do
    @cached_keys = nil if options[:invalidate]
    @cached_keys ||= keys
  end

  claims = JWT.decode(token, nil, true, { algorithm: 'RS256', jwks: jwk_loader })
  self.validate_exp(claims[0]["exp"])
  self.validate_aud(claims[0]["aud"])
end

Private Instance Methods

get_jwk_set() click to toggle source
# File lib/aad_auth.rb, line 21
def get_jwk_set()
  keysUri = "https://login.microsoftonline.com/#{ENV["TENANT_ID"]}/discovery/v2.0/keys?appid=#{ENV["APP_ID"]}"
  response = Net::HTTP.get_response(URI.parse(keysUri))
  if response.code != 200
    raise UnauthorizedError.new("Fail to get JWK Set from Microsoft.")
  else
    keys = JSON.parse(response.body, symbolize_names: true)
  end
end
validate_aud(aud) click to toggle source
# File lib/aad_auth.rb, line 35
def validate_aud(aud)
  raise UnauthorizedError.new("AppID dosen't match with the token aud.") unless aud === ENV["APP_ID"]
end
validate_exp(exp) click to toggle source
# File lib/aad_auth.rb, line 31
def validate_exp(exp)
  raise UnauthorizedError.new("The token has expired.") unless exp > Time.now.to_i
end