Created
January 2, 2012 11:08
-
-
Save jystewart/1550308 to your computer and use it in GitHub Desktop.
Class to help with generating OAuth2 tokens for app-to-app interactions
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
namespace :api do | |
task :create_client => :environment do | |
raise "Requires name. id and secret are optional" unless ENV['name'] | |
@service ||= AuthServiceTools.new | |
@service.create_client(ENV['name'], ENV['id'], ENV['secret']) | |
puts @service.description | |
end | |
task :create_access_token => :environment do | |
raise "Requires name. id and secret are optional" unless ENV['name'] | |
@service ||= AuthServiceTools.new | |
@service.set_client_by_name(ENV['name']) unless @service.client | |
@service.create_token | |
puts @service.description | |
end | |
task :setup_access do | |
raise "Requires name. id and secret are optional" unless ENV['name'] | |
@service ||= AuthServiceTools.new | |
@service.create_client(ENV['name'], ENV['id'], ENV['secret']) | |
@service.create_token | |
puts @service.description | |
end | |
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
class AuthServiceTools | |
attr_accessor :client, :token | |
def create_client(name, id = nil, secret = nil) | |
attrs = {name: name} | |
attrs += {oauth_identifier: id, oauth_secret: secret} if id and secret | |
@client = OAuth2::Provider.client_class.create!(attrs) | |
end | |
def set_client_by_name(name) | |
@client = OAuth2::Provider.client_class.find_by_name!(name) | |
end | |
def create_token(resource_owner = nil, redirect_uri = '#') | |
grant = client.authorizations.create!(resource_owner: resource_owner) | |
code = grant.authorization_codes.create!(redirect_uri: redirect_uri) | |
@token = client.authorization_codes.claim(code.code, code.redirect_uri) | |
end | |
def description | |
output = [] | |
output << "Created OAuth client '#{client.name}'" | |
output << "oauth_id: #{client.oauth_identifier}" | |
output << "oauth_secret: #{client.oauth_secret}" | |
if token | |
output << "access_token: #{token.access_token}" | |
output << "refresh_token: #{token.refresh_token}" | |
end | |
output.join("\n") | |
end | |
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
class ApiController < ApplicationController | |
include OAuth2::Provider::Rails::ControllerAuthentication | |
authenticate_with_oauth | |
def index | |
@allowed_posts = resource_owner.posts | |
end | |
private | |
def resource_owner | |
request.env['oauth2'].resource_owner | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment