artsy-auth 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +59 -0
- data/Rakefile +12 -0
- data/config/initializers/omniauth.rb +3 -0
- data/config/routes.rb +4 -0
- data/lib/artsy-auth/application_controller.rb +23 -0
- data/lib/artsy-auth/config.rb +10 -0
- data/lib/artsy-auth/engine.rb +8 -0
- data/lib/artsy-auth/session_controller.rb +22 -0
- data/lib/artsy-auth/version.rb +3 -0
- data/lib/artsy-auth.rb +8 -0
- metadata +166 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1cf1e6c6b3b49a4acdcd25dccd98ce3ae28a04c7
|
4
|
+
data.tar.gz: 04708d532731280d1043d3613c775351a027f699
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 89a733670ad984119f5a7d03ff96d22ff9fbe15c4f313fcdf3a3623e5d6463fabce0e6aa611528310fcea0786d22b76387eb3153abd0463adcbb4e92f22d73f8
|
7
|
+
data.tar.gz: 87b350bdbb4270269baebe4d50ae9fc7ec63a3e2baebfd3baee6d6e78454d6d3fc57beae4cd472fd8d6c3b059b390216ca8ddbd9b2f2265f9711713f145e61e9
|
data/README.md
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
# Artsy Authentication
|
2
|
+
|
3
|
+
Ruby Gem for adding Artsy's omniauth based authentication to your app.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
Add following line to your Gemfile
|
7
|
+
|
8
|
+
```
|
9
|
+
gem 'artsy-auth'
|
10
|
+
```
|
11
|
+
|
12
|
+
## Usage
|
13
|
+
Artsy Auth is based on [`Rails::Engine`](http://api.rubyonrails.org/classes/Rails/Engine.html).
|
14
|
+
|
15
|
+
### Configure
|
16
|
+
Add `artsy_auth.rb` under `config/initializer`. We need to configure `ArtsyAuth` to use proper Artsy `application_id` and `application_secret`. Also it needs `artsy_url` which will be used to redirect `sign_out` to proper location.
|
17
|
+
`callback_url` defines after a successful omniauth handshake, where should we get redirected to.
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
# config/initalizers/artsy_auth.rb
|
21
|
+
ArtsyAuth.config.artsy_url = 'https://stagingapi.artsy.net'
|
22
|
+
ArtsyAuth.config.callback_url = '/admin'
|
23
|
+
ArtsyAuth.config.application_id = '321322131'
|
24
|
+
ArtsyAuth.config.application_secret = '123123asdasd'
|
25
|
+
```
|
26
|
+
|
27
|
+
You also need to mount session related endpoints to your app, in your `config/routes.rb`. Add following line to your current routes.
|
28
|
+
```ruby
|
29
|
+
# config/routes.rb
|
30
|
+
mount ArtsyAuth::Engine => '/'
|
31
|
+
```
|
32
|
+
|
33
|
+
In order to force authenticaiton, you need to change your `ApplicationController` to inherit from ` ArtsyAuth::ApplicationController`, you also need to add (override) `authorize?` method there which gets a token and in your app you need to define how do you authorize that token, for example:
|
34
|
+
```ruby
|
35
|
+
class ApplicationController < ArtsyAuth::ApplicationController
|
36
|
+
# Prevent CSRF attacks by raising an exception.
|
37
|
+
protect_from_forgery with: :exception
|
38
|
+
|
39
|
+
# override applicaiton to decode token and allow only users with `tester` role
|
40
|
+
def authorized?(token)
|
41
|
+
decoded_token, _headers = JWT.decode(token, 'some-secret')
|
42
|
+
decoded_token['roles'].include? 'tester'
|
43
|
+
end
|
44
|
+
end
|
45
|
+
```
|
46
|
+
Note that this will add authenticaiton to all of your controllers, if you want to skip Artsy's authentication for specific controller you can skip it in your controller by adding:
|
47
|
+
```ruby
|
48
|
+
class TestController
|
49
|
+
skip_before_action :require_artsy_authentication
|
50
|
+
end
|
51
|
+
```
|
52
|
+
|
53
|
+
|
54
|
+
# Contributing
|
55
|
+
|
56
|
+
* Fork the project.
|
57
|
+
* Make your feature addition or bug fix with tests.
|
58
|
+
* Update CHANGELOG.
|
59
|
+
* Send a pull request. Bonus points for topic branches.
|
data/Rakefile
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler/gem_tasks'
|
3
|
+
Bundler.setup(:default, :development)
|
4
|
+
|
5
|
+
require 'rspec/core'
|
6
|
+
require 'rspec/core/rake_task'
|
7
|
+
|
8
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
9
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
10
|
+
end
|
11
|
+
|
12
|
+
task :default => :spec
|
data/config/routes.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
module ArtsyAuth
|
2
|
+
class ApplicationController < ActionController::Base
|
3
|
+
before_action :require_artsy_authentication
|
4
|
+
|
5
|
+
def require_artsy_authentication
|
6
|
+
if session[:access_token]
|
7
|
+
head(:forbidden) unless authorized? session[:access_token]
|
8
|
+
else
|
9
|
+
clear_session_and_reauth! unless session[:access_token]
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def clear_session_and_reauth!
|
14
|
+
reset_session
|
15
|
+
session[:redirect_to] = request.url
|
16
|
+
redirect_to '/auth/artsy'
|
17
|
+
end
|
18
|
+
|
19
|
+
def authorized?(token)
|
20
|
+
raise NotImplementedError
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
module ArtsyAuth
|
2
|
+
mattr_accessor :config
|
3
|
+
end
|
4
|
+
|
5
|
+
ArtsyAuth.config = OpenStruct.new(
|
6
|
+
artsy_url: ENV['artsy_url'] || 'http://localhost:3000',
|
7
|
+
callback_url: ENV['callback_url'] || 'http://localhost:3000/',
|
8
|
+
application_id: ENV['application_id'],
|
9
|
+
application_secret: ENV['application_secret']
|
10
|
+
)
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module ArtsyAuth
|
2
|
+
class SessionsController < ApplicationController
|
3
|
+
skip_before_action :require_artsy_authentication
|
4
|
+
def create
|
5
|
+
session[:user_id] = auth_hash['uid']
|
6
|
+
session[:email] = auth_hash['info']['raw_info']['email']
|
7
|
+
session[:access_token] = auth_hash['credentials']['token']
|
8
|
+
redirect_to "#{ArtsyAuth.config[:callback_url]}"
|
9
|
+
end
|
10
|
+
|
11
|
+
def destroy
|
12
|
+
reset_session
|
13
|
+
redirect_to "#{ArtsyAuth.config[:artsy_url]}/users/sign_out"
|
14
|
+
end
|
15
|
+
|
16
|
+
protected
|
17
|
+
|
18
|
+
def auth_hash
|
19
|
+
request.env['omniauth.auth']
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/lib/artsy-auth.rb
ADDED
metadata
ADDED
@@ -0,0 +1,166 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: artsy-auth
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Artsy
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-03-30 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: omniauth-oauth2
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: omniauth-artsy
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rails
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 4.2.0
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 4.2.0
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: pry
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rspec-rails
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rubocop
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: guard-rubocop
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
description: See summary.
|
126
|
+
email:
|
127
|
+
- it@artsymail.com
|
128
|
+
executables: []
|
129
|
+
extensions: []
|
130
|
+
extra_rdoc_files: []
|
131
|
+
files:
|
132
|
+
- README.md
|
133
|
+
- Rakefile
|
134
|
+
- config/initializers/omniauth.rb
|
135
|
+
- config/routes.rb
|
136
|
+
- lib/artsy-auth.rb
|
137
|
+
- lib/artsy-auth/application_controller.rb
|
138
|
+
- lib/artsy-auth/config.rb
|
139
|
+
- lib/artsy-auth/engine.rb
|
140
|
+
- lib/artsy-auth/session_controller.rb
|
141
|
+
- lib/artsy-auth/version.rb
|
142
|
+
homepage: http://artsy.net
|
143
|
+
licenses: []
|
144
|
+
metadata: {}
|
145
|
+
post_install_message:
|
146
|
+
rdoc_options: []
|
147
|
+
require_paths:
|
148
|
+
- lib
|
149
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
150
|
+
requirements:
|
151
|
+
- - ">="
|
152
|
+
- !ruby/object:Gem::Version
|
153
|
+
version: '0'
|
154
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
155
|
+
requirements:
|
156
|
+
- - ">="
|
157
|
+
- !ruby/object:Gem::Version
|
158
|
+
version: '0'
|
159
|
+
requirements: []
|
160
|
+
rubyforge_project:
|
161
|
+
rubygems_version: 2.4.8
|
162
|
+
signing_key:
|
163
|
+
specification_version: 4
|
164
|
+
summary: ArtsyAuth is a rails based gem that adds Artsy authentication with authorization
|
165
|
+
to your app.
|
166
|
+
test_files: []
|