hobby-auth 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5496a07b70f304e210d3df33809f22fdf8e34210
4
+ data.tar.gz: 18d862103f80a6839f946df723937cf623a83eac
5
+ SHA512:
6
+ metadata.gz: 8ab1798d251e0d244a59af1c00cc037641ea06d0875ef3c30510ea9d793717c883a12e708d1db41c52506d26cd7399af1a3a7c79892a858c186dc108393bd30c
7
+ data.tar.gz: 99e5028ac1329fc1b00e20bf10eb4f7331418d56fdc56b6369fcd24fdb28a20131671342cd8470e8d5098e7a82ebb94b50b22fc15b9ecea1c89357156f37271a
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ Gemfile.lock
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'hobby-devtools', '>=0.0.5'
data/lib/hobby/auth.rb ADDED
@@ -0,0 +1,25 @@
1
+ module Hobby
2
+ module Auth
3
+ def self.[] *user_models
4
+ Module.new do
5
+ define_singleton_method :included do |app|
6
+ user_models.each do |user_model|
7
+ app.define_singleton_method user_model.name.downcase do |route|
8
+ action = route.action
9
+ route.action = -> do
10
+ if user = (user_model.find_by_token env['HTTP_AUTHORIZATION'])
11
+ @user = user
12
+ instance_exec &action
13
+ else
14
+ response.status = 403
15
+ end
16
+ end
17
+ route
18
+ end
19
+ end
20
+ end
21
+ attr_reader :user
22
+ end
23
+ end
24
+ end
25
+ end # Ends should be optional. Block nesting can be inferred from two-space indent.
data/spec/helper.rb ADDED
@@ -0,0 +1,3 @@
1
+ require 'hobby'
2
+ require 'hobby/auth'
3
+ require 'hobby/devtools/rspec_helper'
@@ -0,0 +1,22 @@
1
+ - get:
2
+ path: /
3
+
4
+ response:
5
+ status: 403
6
+
7
+ - get:
8
+ path: /
9
+ headers:
10
+ Authorization: invalid token
11
+
12
+ response:
13
+ status: 403
14
+
15
+ - get:
16
+ path: /
17
+ headers:
18
+ Authorization: first valid getter token
19
+
20
+ response:
21
+ status: 200
22
+ body: oh my get
@@ -0,0 +1,16 @@
1
+ - post:
2
+ path: /
3
+ headers:
4
+ Authorization: first valid getter token
5
+
6
+ response:
7
+ status: 403
8
+
9
+ - post:
10
+ path: /
11
+ headers:
12
+ Authorization: first valid poster token
13
+
14
+ response:
15
+ status: 200
16
+ body: the user's token is first valid poster token
data/spec/http_spec.rb ADDED
@@ -0,0 +1,15 @@
1
+ require 'helper'
2
+
3
+ require_relative 'seed_users'
4
+
5
+ Hobby::Devtools::RSpec.describe do
6
+ app do
7
+ Class.new do
8
+ include Hobby
9
+ include Hobby::Auth[Getter, Poster]
10
+
11
+ getter get { 'oh my get' }
12
+ poster post { "the user's token is #{user.token}" }
13
+ end.new
14
+ end
15
+ end
@@ -0,0 +1,23 @@
1
+ class User
2
+ @@all_users = []
3
+ def initialize token
4
+ @token = token
5
+ @@all_users << self
6
+ end
7
+ attr_reader :token
8
+
9
+ def self.find_by_token token
10
+ @@all_users.find { |user| user.token == token && user.is_a?(self) }
11
+ end
12
+ end
13
+
14
+ class Getter < User
15
+ end
16
+
17
+ class Poster < User
18
+ end
19
+
20
+ Getter.new 'first valid getter token'
21
+ Getter.new 'second valid getter token'
22
+ Poster.new 'first valid poster token'
23
+ Poster.new 'second valid poster token'
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hobby-auth
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Anatoly Chernow
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-12-26 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - ".gitignore"
20
+ - Gemfile
21
+ - lib/hobby/auth.rb
22
+ - spec/helper.rb
23
+ - spec/http/getter.yml
24
+ - spec/http/poster.yml
25
+ - spec/http_spec.rb
26
+ - spec/seed_users.rb
27
+ homepage:
28
+ licenses: []
29
+ metadata: {}
30
+ post_install_message:
31
+ rdoc_options: []
32
+ require_paths:
33
+ - lib
34
+ required_ruby_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ required_rubygems_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ requirements: []
45
+ rubyforge_project:
46
+ rubygems_version: 2.5.2
47
+ signing_key:
48
+ specification_version: 4
49
+ summary: Authorize Hobby routes
50
+ test_files: []