Created
January 5, 2012 05:18
-
-
Save deadprogram/1563834 to your computer and use it in GitHub Desktop.
Hello AT&T Foundry Service
This file contains hidden or 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 File.expand_path("hello", File.dirname(__FILE__)) | |
| run HelloService |
This file contains hidden or 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
| source :rubygems | |
| gem 'rack', "~> 1.3.6" | |
| gem 'sinatra' | |
| gem 'json' | |
| gem 'omniauth' | |
| gem 'omniauth-github' |
This file contains hidden or 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 'rubygems' | |
| require 'sinatra' | |
| require 'json' | |
| require 'omniauth' | |
| require 'omniauth-github' | |
| #require 'omniauth-att' | |
| class HelloService < Sinatra::Base | |
| configure do | |
| set :sessions, true | |
| set :inline_templates, true | |
| end | |
| use OmniAuth::Builder do | |
| provider :github, (ENV['GITHUB_CLIENT_ID']||'ece9da5a3cff23b3475f'), (ENV['GITHUB_CLIENT_SECRET']||'eb81c6098ba5d08e3c2dbd263bf11de5f3382d55') | |
| #provider :att, (ENV['ATT_CLIENT_ID']||'abc'), (ENV['ATT_CLIENT_SECRET']||'123') | |
| end | |
| helpers do | |
| def protected! | |
| unless session[:access_token] | |
| session[:request_path] = request.path | |
| redirect '/auth/github' | |
| else | |
| halt "no" if params[:access_token] && params[:access_token] != session[:access_token] | |
| end | |
| end | |
| def login_url | |
| unless production? | |
| "http://localhost:4567/auth/github" | |
| else | |
| "/auth/github" | |
| end | |
| end | |
| end | |
| get '/' do | |
| erb "<a href='#{login_url}'>Login with Github</a><br><a href='http://localhost:4567/auth/att'>Login with att-foundry</a>" | |
| end | |
| get '/auth/:provider/callback' do | |
| session[:access_token] = request.env['omniauth.auth']['credentials']['token'] | |
| redirect session[:request_path] || '/hello.json' | |
| end | |
| get '/auth/failure' do | |
| session[:request_path] = nil | |
| erb "<h1>Authentication Failed:</h1><h3>message:<h3> <pre>#{params}</pre>" | |
| end | |
| get '/logout' do | |
| session[:access_token] = nil | |
| "Logged out" | |
| end | |
| # the API itself | |
| get '/hello' do | |
| protected! | |
| session[:request_path] = nil | |
| "Hello!" | |
| end | |
| get '/hello.json' do | |
| protected! | |
| session[:request_path] = nil | |
| content_type :json | |
| {:greeting => "Hello!"}.to_json | |
| end | |
| end | |
| HelloService.run! if __FILE__ == $0 | |
| __END__ | |
| @@ layout | |
| <html> | |
| <head> | |
| <link href='http://twitter.github.com/bootstrap/1.4.0/bootstrap.min.css' rel='stylesheet' /> | |
| </head> | |
| <body> | |
| <div class='container'> | |
| <div class="header"> | |
| <ul> | |
| <li><a href="/">home</a></li> | |
| <li><a href="/logout">logout</a></li> | |
| </ul> | |
| </div> | |
| <div class='content'> | |
| <%= yield %> | |
| </div> | |
| </div> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment