auth-service-authenticator 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/.gitignore +10 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +79 -0
- data/Rakefile +6 -0
- data/auth-service-authenticator.gemspec +27 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/auth/service/authenticator.rb +79 -0
- data/lib/auth/service/authenticator/version.rb +7 -0
- metadata +99 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: eed44b8e2547842a1dfde4a5cf7aa89229c723fa
|
4
|
+
data.tar.gz: 881d3ee75fb9b0e69862eab14196b91a7468b990
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4621a47af6b11bb26a6e139029efd63fb634b47e06308bcf97ae58c0e5890959482746591c2abe11af40bb8efbc9a1799f23076d6a84c37c91f67655e2a65a35
|
7
|
+
data.tar.gz: db04c7220e17410173aea965facb1c3716440d169f571a1f57a91dc5f1f7ff4a250e76e43ffabcae1ccc163255569681d608bf90db636b9b4e5c4d8e2f6a2be5
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Bruno Paulino
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
# Auth::Service::Authenticator
|
2
|
+
|
3
|
+
Utilizando a gem 'auth-service-authenticator', você irá plugar o serviço interno de autenticação em sua aplicação, sem necessidade de criação de um serviço de login.
|
4
|
+
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Adicione essa linha ao seu Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'auth-service-authenticator'
|
12
|
+
```
|
13
|
+
|
14
|
+
Agora execute:
|
15
|
+
|
16
|
+
$ bundle install
|
17
|
+
|
18
|
+
## Como Utilizar
|
19
|
+
|
20
|
+
Utilize os metodos `authenticate` e `validate_token`:
|
21
|
+
```ruby
|
22
|
+
# Autentique o usuário fonecendo o email e senha
|
23
|
+
response = Auth::Service::Authenticator::Manager.authenticate('EMAIL', 'SENHA')
|
24
|
+
# Retornará um hash com o seguinte formato:
|
25
|
+
{
|
26
|
+
user: {
|
27
|
+
"auth_token": "1KUg3T0BgEJ_D4W8CIT3sw",
|
28
|
+
"user": {
|
29
|
+
"email": "john@gmail.com",
|
30
|
+
"first_name": "john",
|
31
|
+
"last_name": "Due",
|
32
|
+
"military_rank": 1,
|
33
|
+
"registration": "999999-1",
|
34
|
+
"cpf": "999999999-99"
|
35
|
+
}
|
36
|
+
}
|
37
|
+
}
|
38
|
+
|
39
|
+
# persista o token e utilize-o como uma sessão de usuário
|
40
|
+
token = response[:user]["auth_token"]
|
41
|
+
# salve o token ...
|
42
|
+
|
43
|
+
# Em seus próximos requests, utilize o método abaixo para validar o token
|
44
|
+
response = Auth::Service::Authenticator::Manager.validate_token(token)
|
45
|
+
# Retornará um hash com o seguinte formato caso o token seja válido:
|
46
|
+
{
|
47
|
+
"user": {
|
48
|
+
"email": "john@gmail.com",
|
49
|
+
"first_name": "john",
|
50
|
+
"last_name": "Due",
|
51
|
+
"military_rank": 1,
|
52
|
+
"registration": "999999-1",
|
53
|
+
"cpf": "999999999-99"
|
54
|
+
}
|
55
|
+
}
|
56
|
+
|
57
|
+
# caso o token seja inválido:
|
58
|
+
{
|
59
|
+
:error: "token invalido.",
|
60
|
+
:status: "422",
|
61
|
+
:message: "Unprocessable Entity"
|
62
|
+
}
|
63
|
+
|
64
|
+
# Caso o token seja válido, continue com sua aplicação normalmente.
|
65
|
+
# Caso inválido, redirecione o usuário para página de login.
|
66
|
+
```
|
67
|
+
|
68
|
+
## Development
|
69
|
+
|
70
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
71
|
+
|
72
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
73
|
+
|
74
|
+
## Contributing
|
75
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/brunojppb/auth-service-authenticator.
|
76
|
+
|
77
|
+
## License
|
78
|
+
|
79
|
+
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,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'auth/service/authenticator/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "auth-service-authenticator"
|
8
|
+
spec.version = Auth::Service::Authenticator::VERSION
|
9
|
+
spec.authors = ["Bruno Paulino"]
|
10
|
+
spec.email = ["brunojppb@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Autenticador para serviços internos}
|
13
|
+
spec.description = %q{Utilizando a gem 'auth-service-authenticator', você irá plugar o serviço de autenticação em sua aplicação, sem necessidade de criação de um serviço de login }
|
14
|
+
spec.homepage = "http://www.pm.pb.gov.br/"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
19
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
20
|
+
spec.bindir = "exe"
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.12"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
27
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "auth/service/authenticator"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
require "auth/service/authenticator/version"
|
2
|
+
require 'yaml'
|
3
|
+
require "json"
|
4
|
+
require 'net/http'
|
5
|
+
|
6
|
+
module Auth
|
7
|
+
module Service
|
8
|
+
module Authenticator
|
9
|
+
|
10
|
+
class Manager
|
11
|
+
|
12
|
+
@@auth_url = ''
|
13
|
+
@@auth_port = ''
|
14
|
+
|
15
|
+
# Authenticate user
|
16
|
+
def self.authenticate(email, password)
|
17
|
+
self.configure
|
18
|
+
uri = URI.parse("#{@@auth_url}")
|
19
|
+
http = Net::HTTP.new(@@auth_url, @@auth_port)
|
20
|
+
request = Net::HTTP::Post.new("/api/v1/authenticate")
|
21
|
+
request.add_field('Content-Type', 'application/json')
|
22
|
+
request.body = {'email': email, 'password': password}.to_json
|
23
|
+
response = http.request(request)
|
24
|
+
|
25
|
+
result = case response
|
26
|
+
when Net::HTTPSuccess then
|
27
|
+
{ user: JSON.parse(response.body), status: response.code, message: '' }
|
28
|
+
when Net::HTTPClientError then
|
29
|
+
{ error: 'usuario ou senha inválidos.', status: response.code, message: response.message }
|
30
|
+
when Net::ReadTimeout then
|
31
|
+
{ error: 'timeout', status: response.code, message: response.message }
|
32
|
+
else
|
33
|
+
{ error: 'erro inesperado. tente novamente mais tarde.', status: '422', message: '' }
|
34
|
+
end
|
35
|
+
return result
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.validate_token(token)
|
39
|
+
uri = URI.parse("#{@@auth_url}")
|
40
|
+
http = Net::HTTP.new(@@auth_url, @@auth_port)
|
41
|
+
request = Net::HTTP::Post.new("/api/v1/validate")
|
42
|
+
request.add_field('Content-Type', 'application/json')
|
43
|
+
request.add_field('Authorization', token)
|
44
|
+
response = http.request(request)
|
45
|
+
|
46
|
+
result = case response
|
47
|
+
when Net::HTTPSuccess then
|
48
|
+
{ user: JSON.parse(response.body), status: response.code, message: '' }
|
49
|
+
when Net::HTTPClientError then
|
50
|
+
{ error: 'token invalido.', status: response.code, message: response.message }
|
51
|
+
when Net::ReadTimeout then
|
52
|
+
{ error: 'timeout', status: response.code, message: response.message }
|
53
|
+
else
|
54
|
+
{ error: 'erro inesperado. tente novamente mais tarde.', status: '422', message: '' }
|
55
|
+
end
|
56
|
+
return result
|
57
|
+
end
|
58
|
+
|
59
|
+
private
|
60
|
+
# Setup auth server configuration
|
61
|
+
def self.configure
|
62
|
+
if @@auth_url == ''
|
63
|
+
begin
|
64
|
+
# Load app configuration for server URI in you rails app
|
65
|
+
config = YAML.load_file('config/authenticator_config.yml')
|
66
|
+
@@auth_url = config['url']
|
67
|
+
@@auth_port = config['port']
|
68
|
+
rescue
|
69
|
+
# default configuration
|
70
|
+
@@auth_url = 'localhost'
|
71
|
+
@@auth_port = '4000'
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
metadata
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: auth-service-authenticator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Bruno Paulino
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-01-29 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.12'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.12'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description: 'Utilizando a gem ''auth-service-authenticator'', você irá plugar o serviço
|
56
|
+
de autenticação em sua aplicação, sem necessidade de criação de um serviço de login '
|
57
|
+
email:
|
58
|
+
- brunojppb@gmail.com
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- ".rspec"
|
65
|
+
- ".travis.yml"
|
66
|
+
- Gemfile
|
67
|
+
- LICENSE.txt
|
68
|
+
- README.md
|
69
|
+
- Rakefile
|
70
|
+
- auth-service-authenticator.gemspec
|
71
|
+
- bin/console
|
72
|
+
- bin/setup
|
73
|
+
- lib/auth/service/authenticator.rb
|
74
|
+
- lib/auth/service/authenticator/version.rb
|
75
|
+
homepage: http://www.pm.pb.gov.br/
|
76
|
+
licenses:
|
77
|
+
- MIT
|
78
|
+
metadata: {}
|
79
|
+
post_install_message:
|
80
|
+
rdoc_options: []
|
81
|
+
require_paths:
|
82
|
+
- lib
|
83
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - ">="
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
requirements: []
|
94
|
+
rubyforge_project:
|
95
|
+
rubygems_version: 2.4.5.1
|
96
|
+
signing_key:
|
97
|
+
specification_version: 4
|
98
|
+
summary: Autenticador para serviços internos
|
99
|
+
test_files: []
|