-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1122b52
commit 0c7eb97
Showing
12 changed files
with
209 additions
and
75 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,29 @@ | ||
class PizzasController < ApplicationController | ||
before_filter :oauth_authorized | ||
|
||
def index | ||
render json: {action: :index} | ||
end | ||
|
||
def show | ||
render json: {action: :show} | ||
end | ||
|
||
def create | ||
render json: {action: :create} | ||
end | ||
|
||
def update | ||
render json: {action: :update} | ||
end | ||
|
||
def destroy | ||
render json: {action: :destroy} | ||
end | ||
|
||
private | ||
|
||
def oauth_authorized | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,14 @@ | ||
# TODO: organize this in a Lelylan::Oauth::Scope class so | ||
# that you can give it a more modular structure | ||
|
||
module Lelylan | ||
module Oauth | ||
module Scope | ||
|
||
SCOPE = %w( | ||
type.read type.write | ||
property.read property.write | ||
function.read function.write | ||
status.read status.write | ||
) | ||
|
||
MATCHES = { | ||
write: SCOPE, | ||
read: %w(type.read property.read function.read status.read), | ||
type: %w(type.read type.write), | ||
property: %w(property.read property.write), | ||
function: %w(function.read function.write), | ||
status: %w(status.read status.write) | ||
} | ||
|
||
def self.normalize(scope = []) | ||
normalized = scope.clone | ||
scope.each { |key| normalized << MATCHES[key.to_sym] } | ||
normalized.flatten! | ||
intersection = normalized & SCOPE | ||
return intersection | ||
end | ||
|
||
module Oauth | ||
|
||
def self.normalize_scope(scope) | ||
scope = scope.split(" ") if scope.kind_of? String | ||
normalized = Scope.any_in(name: scope) | ||
normalized = normalized.map(&:values).flatten | ||
if normalized.empty? | ||
return scope | ||
else | ||
return scope + self.normalize_scope(normalized) | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
token_expires_in: "1800" | ||
authorization_expires_in: "150" | ||
random_length: 32 | ||
scope_separator: " " |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,138 @@ | ||
require 'spec_helper' | ||
|
||
describe "Lelylan::Oauth::Scope" do | ||
describe "Oauth" do | ||
before { Scope.destroy_all } | ||
|
||
context "when normalizing key" do | ||
context "#write" do | ||
let(:scope) { Lelylan::Oauth::Scope.normalize(["write"]) } | ||
it { scope.should == Lelylan::Oauth::Scope::MATCHES[:write] } | ||
end | ||
before { @scope = Factory(:scope_pizzas_read) } | ||
before { @scope = Factory(:scope_pizzas_all) } | ||
before { @scope = Factory(:scope_pastas_read) } | ||
before { @scope = Factory(:scope_pastas_all) } | ||
before { @scope = Factory(:scope_read) } | ||
before { @scope = Factory(:scope_all) } | ||
|
||
context "#read" do | ||
let(:scope) { Lelylan::Oauth::Scope.normalize(["read"]) } | ||
it { scope.should == Lelylan::Oauth::Scope::MATCHES[:read] } | ||
end | ||
context "#normalize_scope" do | ||
context "single resource" do | ||
context "single action" do | ||
let(:normalized) { Oauth.normalize_scope("pizzas/index") } | ||
subject { normalized } | ||
it { should include "pizzas/index" } | ||
end | ||
|
||
context "#type" do | ||
let(:scope) { Lelylan::Oauth::Scope.normalize(["type"]) } | ||
it { scope.should == ["type.read", "type.write"] } | ||
end | ||
context "read actions" do | ||
let(:normalized) { Oauth.normalize_scope("pizzas/read") } | ||
subject { normalized } | ||
|
||
context "#property" do | ||
let(:scope) { Lelylan::Oauth::Scope.normalize(["property"]) } | ||
it { scope.should == ["property.read", "property.write"] } | ||
end | ||
it { should include "pizzas/index" } | ||
it { should include "pizzas/show" } | ||
it { should_not include "pizzas/create"} | ||
end | ||
|
||
context "read actions and create action" do | ||
let(:normalized) { Oauth.normalize_scope("pizzas/read pizzas/create") } | ||
subject { normalized } | ||
|
||
it { should include "pizzas/index" } | ||
it { should include "pizzas/show" } | ||
it { should include "pizzas/create"} | ||
end | ||
|
||
context "all rest actions" do | ||
let(:normalized) { Oauth.normalize_scope("pizzas") } | ||
subject { normalized } | ||
|
||
context "#function" do | ||
let(:scope) { Lelylan::Oauth::Scope.normalize(["function"]) } | ||
it { scope.should == ["function.read", "function.write"] } | ||
it { should include "pizzas/index" } | ||
it { should include "pizzas/show" } | ||
it { should include "pizzas/create" } | ||
it { should include "pizzas/update" } | ||
it { should include "pizzas/destroy"} | ||
it { should_not include "pastas/index" } | ||
end | ||
end | ||
|
||
context "#status" do | ||
let(:scope) { Lelylan::Oauth::Scope.normalize(["status"]) } | ||
it { scope.should == ["status.read", "status.write"] } | ||
context "all resources" do | ||
context "single actions" do | ||
let(:normalized) { Oauth.normalize_scope("pizzas/index pastas/index") } | ||
subject { normalized } | ||
it { should include "pizzas/index" } | ||
it { should_not include "pizzas/show" } | ||
it { should include "pastas/index" } | ||
it { should_not include "pastas/show" } | ||
end | ||
|
||
context "read actions" do | ||
let(:normalized) { Oauth.normalize_scope("read") } | ||
subject { normalized } | ||
|
||
it { should include "pizzas/index" } | ||
it { should include "pizzas/show" } | ||
it { should_not include "pizzas/create"} | ||
it { should include "pastas/index" } | ||
it { should include "pastas/show" } | ||
it { should_not include "pastas/create"} | ||
end | ||
|
||
context "all rest actions" do | ||
let(:normalized) { Oauth.normalize_scope("all") } | ||
subject { normalized } | ||
|
||
it { should include "pizzas/index" } | ||
it { should include "pizzas/show" } | ||
it { should include "pizzas/create" } | ||
it { should include "pizzas/update" } | ||
it { should include "pizzas/destroy"} | ||
|
||
it { should include "pastas/index" } | ||
it { should include "pastas/show" } | ||
it { should include "pastas/create" } | ||
it { should include "pastas/update" } | ||
it { should include "pastas/destroy"} | ||
end | ||
end | ||
end | ||
|
||
context "when normalizing bases" do | ||
let(:scope) { Lelylan::Oauth::Scope.normalize(["status.read", "property.write"]) } | ||
it { scope.should == ["status.read", "property.write"]} | ||
end | ||
end | ||
|
||
context "when normalizing not existing keys" do | ||
let(:scope) { Lelylan::Oauth::Scope.normalize(["status.read", "resource.not_existing"]) } | ||
it { scope.should == ["status.read"]} | ||
end | ||
|
||
end | ||
|
||
|
||
#context "when normalizing key" do | ||
#context "#write" do | ||
#let(:scope) { Lelylan::Oauth::Scope.normalize(["write"]) } | ||
#it { scope.should == Lelylan::Oauth::Scope::MATCHES[:write] } | ||
#end | ||
|
||
#context "#read" do | ||
#let(:scope) { Lelylan::Oauth::Scope.normalize(["read"]) } | ||
#it { scope.should == Lelylan::Oauth::Scope::MATCHES[:read] } | ||
#end | ||
|
||
#context "#type" do | ||
#let(:scope) { Lelylan::Oauth::Scope.normalize(["type"]) } | ||
#it { scope.should == ["type.read", "type.write"] } | ||
#end | ||
|
||
#context "#property" do | ||
#let(:scope) { Lelylan::Oauth::Scope.normalize(["property"]) } | ||
#it { scope.should == ["property.read", "property.write"] } | ||
#end | ||
|
||
#context "#function" do | ||
#let(:scope) { Lelylan::Oauth::Scope.normalize(["function"]) } | ||
#it { scope.should == ["function.read", "function.write"] } | ||
#end | ||
|
||
#context "#status" do | ||
#let(:scope) { Lelylan::Oauth::Scope.normalize(["status"]) } | ||
#it { scope.should == ["status.read", "status.write"] } | ||
#end | ||
#end | ||
|
||
#context "when normalizing bases" do | ||
#let(:scope) { Lelylan::Oauth::Scope.normalize(["status.read", "property.write"]) } | ||
#it { scope.should == ["status.read", "property.write"]} | ||
#end | ||
|
||
#context "when normalizing not existing keys" do | ||
#let(:scope) { Lelylan::Oauth::Scope.normalize(["status.read", "resource.not_existing"]) } | ||
#it { scope.should == ["status.read"]} | ||
#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
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
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