Sha256: a00a2b7cf5e40b1a7d49d6264b760f6e8f72ea1948a3ebff6b66c8cb8092ebf1

Contents?: true

Size: 1.21 KB

Versions: 2

Compression:

Stored size: 1.21 KB

Contents

require "rack/oauth2/sinatra"

class MyApp < Sinatra::Base
  use Rack::Logger
  set :sessions, true

  register Rack::OAuth2::Sinatra
  oauth.scopes = %w{read write}
  oauth.authenticator = lambda do |username, password|
    "Superman" if username == "cowbell" && password == "more"
  end
  oauth.database = DATABASE


  # 3.  Obtaining End-User Authorization

  get "/oauth/authorize" do
    session["oauth.authorization"] = oauth.authorization
    "client: #{oauth.client.display_name}\nscope: #{oauth.scope.join(", ")}"
  end

  post "/oauth/grant" do
    oauth.grant! session["oauth.authorization"], "Superman"
  end

  post "/oauth/deny" do
    oauth.deny! session["oauth.authorization"]
  end


  # 5.  Accessing a Protected Resource

  before { @user = oauth.identity if oauth.authenticated? }

  get "/public" do
    if oauth.authenticated?
      "HAI from #{oauth.identity}"
    else
      "HAI"
    end
  end

  oauth_required "/private", "/change"

  get "/private" do
    "Shhhh"
  end

  post "/change" do
    "Woot!"
  end

  oauth_required "/calc", :scope=>"math"

  get "/calc" do
  end

  get "/user" do
    @user
  end

  get "/list_tokens" do
    oauth.list_access_tokens("Superman").map(&:token).join(" ")
  end
  
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rack-oauth2-server-1.1.1 test/sinatra/my_app.rb
rack-oauth2-server-1.1.0 test/sinatra/my_app.rb