Instantly share code, notes, and snippets.
Created
October 15, 2012 01:33
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
-
Save oogali/3890396 to your computer and use it in GitHub Desktop.
meh.
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
| #!/usr/bin/env ruby | |
| require 'rubygems' | |
| require 'sinatra' | |
| require 'json' | |
| require 'rest_client' | |
| require 'haml' | |
| require 'sass' | |
| require 'time' | |
| set :server, :thin | |
| set :port, 1919 | |
| enable :sessions | |
| REAUTH_INTERVAL = 15 | |
| @last_refresh = nil | |
| before do | |
| @auth ||= Rack::Auth::Basic::Request.new request.env | |
| if @auth.provided? and @auth.basic? and @auth.credentials | |
| @username, @password = @auth.credentials | |
| end | |
| if %w[ / /add /delete ].include? request.path and not authorized? | |
| request_auth unless authenticated? | |
| end | |
| end | |
| helpers do | |
| def authorized? | |
| session[:username] and session[:authorized_at] and (Time.now - session[:authorized_at]) < REAUTH_INTERVAL | |
| end | |
| def authenticated? | |
| if get_authorizations | |
| session[:authorized_at] = Time.now | |
| true | |
| end | |
| end | |
| def request_auth | |
| response['WWW-Authenticate'] = %(Basic realm="GitHub Authorizations") | |
| throw :halt, [401, "Unauthorized\n"] | |
| end | |
| def get_authorizations(creds = nil) | |
| begin | |
| github = RestClient::Request.execute :method => :get, :url => 'https://api.github.com/authorizations', :user => @username, :password => @password | |
| rescue RestClient::Unauthorized | |
| return false | |
| rescue RestClient::Forbidden | |
| return false | |
| end | |
| session[:authorizations] = JSON.parse github.to_s | |
| @last_refresh = Time.now | |
| true | |
| end | |
| def create_new_authorization(note, url, scopes) | |
| github = RestClient::Request.execute :method => :post, :url => "https://api.github.com/authorizations", :user => @username, :password => @password | |
| end | |
| def delete_authorization(oid) | |
| begin | |
| github = RestClient::Request.execute :method => :delete, :url => "https://api.github.com/authorizations/#{oid}", :user => @username, :password => @password | |
| rescue | |
| end | |
| get_authorizations | |
| github.code == 204 ? 'OK' : github.to_s | |
| end | |
| end | |
| get '/' do | |
| @authorizations = session[:authorizations] | |
| haml :index | |
| end | |
| get '/stylesheet.css' do | |
| sass :stylesheet | |
| end | |
| post '/add' do | |
| create_new_authorization params[:note], params[:url], params[:scopes] | |
| redirect '/' | |
| end | |
| get '/delete/:oid' do |oid| | |
| delete_authorization oid | |
| end | |
| __END__ | |
| @@ stylesheet | |
| body, h1, h2, h3, h4, h5, h6, input | |
| font-family: 'Helvetica Neue', helvetica, arial, sans-serif | |
| br | |
| clear: both | |
| div.header | |
| font-weight: bold | |
| div.auths | |
| width: 800px | |
| font-size: 0.8em | |
| div.oid | |
| width: 85px | |
| float: left | |
| div.token | |
| width: 100px | |
| float: left | |
| div.note | |
| width: 305px | |
| float: left | |
| div.scopes, div.created | |
| width: 150px | |
| float: left | |
| @@ layout | |
| !!! | |
| %html | |
| %head | |
| %title= 'github authorizations' | |
| %script{ :src => 'https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js' } | |
| %link{ :href => '/stylesheet.css', :rel => :stylesheet } | |
| %body | |
| %h1= 'github authorizations' | |
| %hr | |
| =yield | |
| @@ index | |
| ="#{@authorizations.length} authorization(s) found." | |
| %br | |
| %br | |
| #auths | |
| %div.header | |
| %div.oid= 'ID' | |
| %div.token= 'Token' | |
| %div.note= 'Note' | |
| %div.scopes= 'Scopes' | |
| %div.created= 'Created' | |
| %br | |
| - @authorizations.each do |authorization| | |
| %div.row{ 'data-oid' => authorization['id'].to_s, 'data-token' => authorization['token'] } | |
| %div.oid= authorization['id'].to_s | |
| %div.token= "#{authorization['token'][0..7]}..." | |
| %div.note | |
| %a{ :href => authorization['app']['url'] }= authorization['app']['name'] | |
| %div.scopes | |
| =authorization['scopes'].empty? ? '(none)' : authorization['scopes'].join(', ') | |
| %div.created= Time.parse(authorization['created_at']).strftime('%B %d, %I:%M%p') | |
| %br | |
| %br | |
| :javascript | |
| $(document).ready(function() { | |
| $('div.row').click(function() { | |
| oid = $(this).data('oid'); | |
| if (confirm('Are you sure you want to delete ID ' + oid + '?') == true) { | |
| $.get('/delete/' + oid); | |
| window.location.reload(true); | |
| } | |
| }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment