Last active
August 26, 2016 10:13
-
-
Save nolith/649b9ccd4b5ef54d53f3bda9717e93da to your computer and use it in GitHub Desktop.
OAuth2Checker ruby gem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Gem::Specification.new do |s| | |
s.name = 'oauth2-checker' | |
s.version = '0.1.0' | |
s.summary = "OAuth2Checker" | |
s.description = "Just a simple OAuth2 token info checker" | |
s.authors = ["Sandro Mehic", "Alessio Caiazza"] | |
s.email = 'devs@facts4.work' | |
s.homepage = 'http://facts4workers.eu/' | |
s.files = ["oauth2_checker.rb"] | |
s.add_runtime_dependency 'oauth2', '~> 1.1' | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'oauth2' | |
class OAuth2Checker | |
def initialize(client_id, client_secret, options = {}, &block) | |
@client = OAuth2::Client.new(client_id, client_secret, options, block) | |
end | |
def check_access_token(token) | |
# raise exception if no token exists | |
# at = AccessToken.where(token: token).take | |
# try to check if token is still valid with oauth provider | |
begin | |
# try to do a request to the oauth provider | |
req = @client.request(:get, "oauth/token/info", {:params => {:access_token => token}}) | |
# if i got here, the request was success => token is valid | |
return token | |
rescue | |
# unexisting or expired token, access denied | |
return false | |
end | |
end | |
def get_resource_owner(token) | |
begin | |
req = @client.request(:get, "oauth/token/info", {:params => {:access_token => token}}) | |
return req.parsed["resource_owner_id"] | |
rescue | |
return "undefined" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment