cru-auth-lib 0.1.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: 14b85381650644e3013a5c2c2d4e3078f90ccf57
4
+ data.tar.gz: a37f916e4cb62f39be1e2933c01900290d96165d
5
+ SHA512:
6
+ metadata.gz: 236cb01480553d09b3e47004af7c6da6cdc15febe5afa93f39e2b0410200773a3e9c3b9545954174e8ecc2a299e4d26a41a5c2192181ec6af55254ce935c6a50
7
+ data.tar.gz: 3973ae2070ffaf406e5eb1e1b18493909e4f74bd20a808b438f725becd9f7575c9f90df84e17d0a375917046316d0f2cc55975fcc9355949d9f1f52804c9cbd0
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2017 Cru, Brian Zoetewey
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,21 @@
1
+ # CruAuthLib
2
+ Collection of common auth models for use in shared API authentication.
3
+
4
+ ## Installation
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem 'cru-auth-lib'
9
+ ```
10
+
11
+ And then execute:
12
+ ```bash
13
+ $ bundle
14
+ ```
15
+
16
+ Or install it yourself as:
17
+ ```bash
18
+ $ gem install cru-auth-lib
19
+ ```
20
+ ## License
21
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'cru_auth_lib/version'
4
+ require 'cru_auth_lib/access_token'
5
+ require 'cru_auth_lib/access_token_serializer'
6
+ require 'cru_auth_lib/access_token_protected_concern'
7
+ require 'cru_auth_lib/api_error'
8
+ require 'cru_auth_lib/api_error_serializer'
9
+ require 'redis'
10
+
11
+ module CruAuthLib
12
+ class << self
13
+ attr_accessor :redis_host, :redis_port, :redis_db, :redis_client
14
+
15
+ def configure
16
+ yield self
17
+ end
18
+
19
+ def redis_host
20
+ @redis_host ||= 'localhost'
21
+ end
22
+
23
+ def redis_port
24
+ @redis_port ||= '6379'
25
+ end
26
+
27
+ def redis_db
28
+ @redis_db ||= 2
29
+ end
30
+
31
+ def redis_client
32
+ ::Redis.new(host: CruAuthLib.redis_host,
33
+ port: CruAuthLib.redis_port,
34
+ db: CruAuthLib.redis_db)
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_model_serializers'
4
+ require 'securerandom'
5
+
6
+ module CruAuthLib
7
+ class AccessToken < ActiveModelSerializers::Model
8
+ attr_accessor :key_guid, :relay_guid, :guid, :email, :first_name, :last_name, :token, :pgt
9
+
10
+ def initialize(attributes = {})
11
+ attributes.symbolize_keys!
12
+ super(attributes)
13
+ @token = generate_access_token unless attributes[:token]
14
+ write
15
+ end
16
+
17
+ class << self
18
+ def read(token)
19
+ json = exist?(token)
20
+ new(Oj.load(json)) if json
21
+ end
22
+
23
+ def exist?(token)
24
+ redis_client.get(redis_key(token))
25
+ end
26
+
27
+ def redis_client
28
+ @redis_client ||= CruAuthLib.redis_client
29
+ end
30
+
31
+ def redis_key(token)
32
+ ['cru_lib', 'access_token', token].join(':')
33
+ end
34
+
35
+ def del(token)
36
+ redis_client.del(redis_key(token))
37
+ end
38
+ end
39
+
40
+ private
41
+
42
+ def generate_access_token
43
+ loop do
44
+ attributes[:token] = SecureRandom.uuid.delete('-')
45
+ break unless self.class.exist?(attributes[:token])
46
+ end
47
+ attributes[:token]
48
+ end
49
+
50
+ def write
51
+ self.class.redis_client.setex(self.class.redis_key(attributes[:token]), 30.minutes.to_i, to_json)
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CruAuthLib
4
+ module AccessTokenProtectedConcern
5
+ extend ActiveSupport::Concern
6
+
7
+ protected
8
+
9
+ def authenticate_request
10
+ authenticate_token || render_unauthorized
11
+ end
12
+
13
+ def authenticate_token
14
+ token = oauth_access_token_from_header
15
+ return unless oauth_access_token_from_header
16
+ @access_token = AccessToken.read(token)
17
+ end
18
+
19
+ # grabs access_token from header if one is present
20
+ def oauth_access_token_from_header
21
+ auth_header = request.env['HTTP_AUTHORIZATION'] || ''
22
+ match = auth_header.match(/^Bearer\s(.*)/)
23
+ return match[1] if match.present?
24
+ false
25
+ end
26
+
27
+ def render_unauthorized
28
+ headers['WWW-Authenticate'] = %(CAS realm="Application")
29
+ render_error('Bad token', status: 401)
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CruAuthLib
4
+ class AccessTokenSerializer < ActiveModel::Serializer
5
+ attributes :key_guid, :email, :first_name, :last_name, :token
6
+
7
+ def _type
8
+ 'access_token'
9
+ end
10
+
11
+ def id
12
+ object.token
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CruAuthLib
4
+ class ApiError < ActiveModelSerializers::Model
5
+ attr_accessor :message, :options
6
+ end
7
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CruAuthLib
4
+ class ApiErrorSerializer < ActiveModel::Serializer
5
+ attributes :message
6
+
7
+ def _type
8
+ 'api_error'
9
+ end
10
+
11
+ def id
12
+ _type
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CruAuthLib
4
+ VERSION = '0.1.0'
5
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cru-auth-lib
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Brian Zoetewey
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-05-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: active_model_serializers
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 0.10.0.rc1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 0.10.0.rc1
27
+ - !ruby/object:Gem::Dependency
28
+ name: redis
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.3'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.3'
41
+ description: Collection of common auth models for use in shared authentication.
42
+ email:
43
+ - brian.zoetewey@cru.org
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - MIT-LICENSE
49
+ - README.md
50
+ - Rakefile
51
+ - lib/cru_auth_lib.rb
52
+ - lib/cru_auth_lib/access_token.rb
53
+ - lib/cru_auth_lib/access_token_protected_concern.rb
54
+ - lib/cru_auth_lib/access_token_serializer.rb
55
+ - lib/cru_auth_lib/api_error.rb
56
+ - lib/cru_auth_lib/api_error_serializer.rb
57
+ - lib/cru_auth_lib/version.rb
58
+ homepage: https://github.com/CruGlobal/cru-auth-lib
59
+ licenses:
60
+ - MIT
61
+ metadata: {}
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubyforge_project:
78
+ rubygems_version: 2.6.8
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: Cru Auth API library
82
+ test_files: []