Skip to content
Newer
Older
100644 60 lines (41 sloc) 1.3 KB
c189385 @ismasan Readme
authored
1 # Rack OAuth Utils
2
3 Simple Rack middleware that catches OAuth2 access tokens and validates identity
4
5 This gem only covers the simple use of "using a token". You must implement the authorization and "getting a token" part in your app.
6
7 ## USAGE
8445cad @ismasan Trying out Github syntax highlighting
authored
8 ```ruby
8261d7d @ismasan S highlight 2
authored
9 class API < Sinatra::Base
10
11 use Rack::OAuth2Utils::Middleware do |access_token|
12 AccessToken.find_by_token(access_token).try :account_id
13 end
14
15 helpers do
c189385 @ismasan Readme
authored
16
8261d7d @ismasan S highlight 2
authored
17 def authorized?
18 !!identity
19 end
20
21 def identity
22 requets.env['oauth.identity']
23 end
24
25 def current_account
26 Account.find(identity) if authorized?
27 end
28
29 end
30
31 get '/private' do
32 if authorized?
33 content_type 'application/json'
34 current_account.to_json
35 else
36 halt 403, 'Access forbidden'
c189385 @ismasan Readme
authored
37 end
8261d7d @ismasan S highlight 2
authored
38 end
39
40 end
8445cad @ismasan Trying out Github syntax highlighting
authored
41 ```
c189385 @ismasan Readme
authored
42
be195e4 @ismasan Passing identity resolver block to middleware, ala Rack::Auth::Basic
authored
43 Rack::OAuth2Utils::Middleware takes a block with the request's access token. YOu can use it to resolve it to an identity string (ie a user or account id).
c189385 @ismasan Readme
authored
44
45 There is a test store based on PStore (filesystem. Do no use in production):
9764b22 @ismasan Fixed syntax h.
authored
46
8445cad @ismasan Trying out Github syntax highlighting
authored
47 ```ruby
8261d7d @ismasan S highlight 2
authored
48 STORE = Rack::OAuth2Utils::TestStore.new('tmp/access_tokens.store')
49
50 STORE['foobar'] = 'some_identity'
51
52 use Rack::OAuth2Utils::Middleware do |access_token|
53 STORE[access_token]
54 end
8445cad @ismasan Trying out Github syntax highlighting
authored
55 ```
9764b22 @ismasan Fixed syntax h.
authored
56
c189385 @ismasan Readme
authored
57 It is up to you how you store tokens and identities.
58
59
60 See test/middlewate_test.rb for details
Something went wrong with that request. Please try again.