Skip to content

Instantly share code, notes, and snippets.

@craigplummer
Created July 29, 2016 15:41
Show Gist options
  • Save craigplummer/92a428899ad9e1c25b93385884b4793f to your computer and use it in GitHub Desktop.
Save craigplummer/92a428899ad9e1c25b93385884b4793f to your computer and use it in GitHub Desktop.
Using Microsoft Azure AD for API Authentication with Rails and Warden - azure_ad_json_web_token.rb
class AzureAdJsonWebToken
def self.rsa_key
url = URI.parse('https://login.windows.net/common/discovery/keys')
key_file = JSON.parse(Net::HTTP.get(url))
x5c = Base64.decode64(key_file['keys'][0]['x5c'][0])
OpenSSL::X509::Certificate.new(x5c).public_key
end
def self.aud
ENV['aud']
end
def self.iss
ENV['iss']
end
def self.decode(token)
JWT.decode(token, rsa_key, true, { algorithm: 'RS256',
aud: aud,
verify_aud: true,
iss: iss,
verify_iss: true })
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment