glogin 0.5.1 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a5cea77d58ede219ba077e49630a46bb098be2ddc118ee877d6f4546abdf34e8
4
- data.tar.gz: 655bd2fc1ef553c2ade646ab881faa8b681fe9c3357bf28bf015cb61d936201f
3
+ metadata.gz: 72c86f15831f8df5a5d22617b889445f2edc8a18b9a8cac412ecd6fae3fb10ea
4
+ data.tar.gz: 9280242c989853567637bdaeaed594dcc6f728d16c0f6bf176168c4b69f3166d
5
5
  SHA512:
6
- metadata.gz: eeb4672fe51e49f717fdfae2b4f7edec1fd5fb3a95a3db53e06a57a8fa6650aa25a4f0f4772168c0a76703a52c5ec5e5ae7f078ccbc185b54755daa4ee997dca
7
- data.tar.gz: 3c81ba17842a04dd9f8c45e46fb795c6dac8b6e3375ef797f77d7c8db4b7eb76eb777a0fc46af73c3cd0a07f4ea10259d1a5fe4c62ae9309d534ee71dfd9f261
6
+ metadata.gz: 6d4437fd9ab2f4634b248aaf9f23d4dd32bc026bfa69660a56aaf171418de27972f4304cac309dceba16171b57bead9839d9e24f350782285a69d3a191bf47af
7
+ data.tar.gz: 87647de3454925e2eb32baa1d209e9eb3e1d353c922fdca6e1e6a22cf6b8d9114c971fc9a8e1feaa018aded19cf2ac91fd33a9dcd13de5c06a4a13cedb574a79
@@ -7,6 +7,8 @@ AllCops:
7
7
 
8
8
  Metrics/MethodLength:
9
9
  Enabled: false
10
+ Style/ClassAndModuleChildren:
11
+ Enabled: false
10
12
  Layout/MultilineMethodCallIndentation:
11
13
  Enabled: false
12
14
  Metrics/AbcSize:
@@ -1,15 +1,13 @@
1
1
  assets:
2
- rubygems.yml: zerocracy/home#assets/rubygems.yml
3
- s3cfg: zerocracy/home#assets/s3cfg
2
+ rubygems.yml: yegor256/home#assets/rubygems.yml
4
3
  install: |
5
4
  export GEM_HOME=~/.ruby
6
5
  export GEM_PATH=$GEM_HOME:$GEM_PATH
7
- sudo apt-get -y update
8
- sudo gem install pdd
6
+ sudo gem install pdd -v 0.20.5
7
+ bundle install
9
8
  release:
10
9
  script: |-
11
- bundle install
12
- rake
10
+ bundle exec rake
13
11
  rm -rf *.gem
14
12
  sed -i "s/1\.0\.snapshot/${tag}/g" lib/glogin/version.rb
15
13
  git add lib/glogin/version.rb
@@ -17,15 +15,10 @@ release:
17
15
  gem build glogin.gemspec
18
16
  chmod 0600 ../rubygems.yml
19
17
  gem push *.gem --config-file ../rubygems.yml
20
- commanders:
21
- - yegor256
22
- architect:
23
- - yegor256
24
18
  merge:
25
19
  script: |-
26
- bundle install
27
- rake
28
- pdd
20
+ bundle exec rake
21
+ pdd -f /dev/null
29
22
  deploy:
30
23
  script: |-
31
24
  echo "There is nothing to deploy"
@@ -8,6 +8,6 @@ branches:
8
8
  install:
9
9
  - travis_retry bundle update
10
10
  script:
11
- - rake
11
+ - bundle exec rake
12
12
  after_success:
13
13
  - "bash <(curl -s https://codecov.io/bash)"
data/README.md CHANGED
@@ -1,6 +1,8 @@
1
+ <img src="/logo.svg" width="64px" height="64px"/>
2
+
1
3
  [![Managed by Zerocracy](https://www.0crat.com/badge/C3RFVLU72.svg)](https://www.0crat.com/p/C3RFVLU72)
2
4
  [![DevOps By Rultor.com](http://www.rultor.com/b/yegor256/glogin)](http://www.rultor.com/p/yegor256/glogin)
3
- [![We recommend RubyMine](http://www.elegantobjects.org/rubymine.svg)](https://www.jetbrains.com/ruby/)
5
+ [![We recommend RubyMine](https://www.elegantobjects.org/rubymine.svg)](https://www.jetbrains.com/ruby/)
4
6
 
5
7
  [![Build Status](https://travis-ci.org/yegor256/glogin.svg)](https://travis-ci.org/yegor256/glogin)
6
8
  [![PDD status](http://www.0pdd.com/svg?name=yegor256/glogin)](http://www.0pdd.com/p?name=yegor256/glogin)
@@ -9,7 +11,8 @@
9
11
  [![Test Coverage](https://img.shields.io/codecov/c/github/yegor256/glogin.svg)](https://codecov.io/github/yegor256/glogin?branch=master)
10
12
  [![Yard Docs](http://img.shields.io/badge/yard-docs-blue.svg)](http://rubydoc.info/github/yegor256/glogin/master/frames)
11
13
 
12
- ## GitHub Login for Ruby web app
14
+ [![Hits-of-Code](https://hitsofcode.com/github/yegor256/glogin)](https://hitsofcode.com/view/github/yegor256/glogin)
15
+ [![License](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/yegor256/glogin/blob/master/LICENSE.txt)
13
16
 
14
17
  This simple gem will help you enable login/logout through
15
18
  [GitHub OAuth](https://developer.github.com/apps/building-integrations/setting-up-and-registering-oauth-apps/)
@@ -17,6 +20,9 @@ for your web application. This is how it works with
17
20
  [Sinatra](http://www.sinatrarb.com/),
18
21
  but you can do something similar in any framework.
19
22
 
23
+ Read this blog post to get the idea:
24
+ [_Simplified GitHub Login for a Ruby Web App_](https://www.yegor256.com/2018/06/19/glogin.html)
25
+
20
26
  First, somewhere in the global space, before the app starts:
21
27
 
22
28
  ```ruby
@@ -47,7 +53,7 @@ before '/*' do
47
53
  # encrypt the value in the cookie.
48
54
  secret
49
55
  ).to_user
50
- rescue OpenSSL::Cipher::CipherError => _
56
+ rescue GLogin::Codec::DecodingError => _
51
57
  # Nothing happens here, the user is not logged in.
52
58
  cookies.delete(:glogin)
53
59
  end
@@ -132,7 +138,7 @@ encrypted = codec.encrypt('Hello, world!')
132
138
  decrypted = codec.decrypt(encrypted)
133
139
  ```
134
140
 
135
- # How to contribute
141
+ ## How to contribute
136
142
 
137
143
  Read [these guidelines](https://www.yegor256.com/2014/04/15/github-guidelines.html).
138
144
  Make sure you build is green before you contribute
@@ -141,31 +147,7 @@ your pull request. You will need to have [Ruby](https://www.ruby-lang.org/en/) 2
141
147
 
142
148
  ```
143
149
  $ bundle update
144
- $ rake
150
+ $ bundle exec rake
145
151
  ```
146
152
 
147
153
  If it's clean and you don't see any error messages, submit your pull request.
148
-
149
- ## License
150
-
151
- (The MIT License)
152
-
153
- Copyright (c) 2017-2019 Yegor Bugayenko
154
-
155
- Permission is hereby granted, free of charge, to any person obtaining a copy
156
- of this software and associated documentation files (the 'Software'), to deal
157
- in the Software without restriction, including without limitation the rights
158
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
159
- copies of the Software, and to permit persons to whom the Software is
160
- furnished to do so, subject to the following conditions:
161
-
162
- The above copyright notice and this permission notice shall be included in all
163
- copies or substantial portions of the Software.
164
-
165
- THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
166
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
167
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
168
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
169
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
170
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
171
- SOFTWARE.
data/Rakefile CHANGED
@@ -63,7 +63,7 @@ RuboCop::RakeTask.new(:rubocop) do |task|
63
63
  end
64
64
 
65
65
  task :copyright do
66
- sh "grep -q -r '#{Date.today.strftime('%Y')}' \
66
+ sh "grep -q -r '2017-#{Date.today.strftime('%Y')}' \
67
67
  --include '*.rb' \
68
68
  --include '*.txt' \
69
69
  --include 'Rakefile' \
@@ -47,6 +47,7 @@ Gem::Specification.new do |s|
47
47
  s.test_files = s.files.grep(%r{^(test)/})
48
48
  s.rdoc_options = ['--charset=UTF-8']
49
49
  s.extra_rdoc_files = ['README.md', 'LICENSE.txt']
50
+ s.add_runtime_dependency 'base58', '0.2.3'
50
51
  s.add_development_dependency 'codecov', '0.1.14'
51
52
  s.add_development_dependency 'minitest', '5.11.3'
52
53
  s.add_development_dependency 'rake', '12.3.2'
@@ -24,63 +24,64 @@
24
24
  require 'securerandom'
25
25
  require 'openssl'
26
26
  require 'digest/sha1'
27
- require 'base64'
27
+ require 'base58'
28
28
 
29
29
  # Codec.
30
30
  # Author:: Yegor Bugayenko (yegor256@gmail.com)
31
31
  # Copyright:: Copyright (c) 2017-2019 Yegor Bugayenko
32
32
  # License:: MIT
33
- module GLogin
34
- #
35
- # Codec
36
- #
37
- class Codec
38
- def initialize(secret = '')
39
- raise 'Secret can\'t be nil' if secret.nil?
40
- @secret = secret
41
- end
33
+ class GLogin::Codec
34
+ # When can't decode.
35
+ class DecodingError < StandardError; end
42
36
 
43
- def decrypt(text)
44
- raise 'Text can\'t be nil' if text.nil?
45
- if @secret.empty?
46
- text
47
- else
48
- cpr = cipher
49
- cpr.decrypt
50
- cpr.key = digest
51
- plain = Base64.decode64(text)
52
- raise OpenSSL::Cipher::CipherError if plain.empty?
53
- decrypted = cpr.update(plain)
54
- decrypted << cpr.final
55
- salt, body = decrypted.to_s.split(' ', 2)
56
- raise OpenSSL::Cipher::CipherError if salt.empty?
57
- raise OpenSSL::Cipher::CipherError if body.nil?
58
- body.force_encoding('UTF-8')
59
- body
60
- end
61
- end
37
+ def initialize(secret = '')
38
+ raise 'Secret can\'t be nil' if secret.nil?
39
+ @secret = secret
40
+ end
62
41
 
63
- def encrypt(text)
64
- raise 'Text can\'t be nil' if text.nil?
65
- if @secret.empty?
66
- text
67
- else
68
- cpr = cipher
69
- cpr.encrypt
70
- cpr.key = digest
71
- salt = SecureRandom.base64(Random.rand(8..32))
72
- encrypted = cpr.update(salt + ' ' + text)
73
- encrypted << cpr.final
74
- Base64.encode64(encrypted.to_s).delete("\n")
75
- end
42
+ def decrypt(text)
43
+ raise 'Text can\'t be nil' if text.nil?
44
+ if @secret.empty?
45
+ text
46
+ else
47
+ cpr = cipher
48
+ cpr.decrypt
49
+ cpr.key = digest
50
+ raise DecodingError unless /^[a-zA-Z0-9]+$/.match?(text)
51
+ plain = Base58.base58_to_binary(text)
52
+ raise DecodingError if plain.empty?
53
+ decrypted = cpr.update(plain)
54
+ decrypted << cpr.final
55
+ salt, body = decrypted.to_s.split(' ', 2)
56
+ raise DecodingError if salt.empty?
57
+ raise DecodingError if body.nil?
58
+ body.force_encoding('UTF-8')
59
+ body
76
60
  end
61
+ rescue OpenSSL::Cipher::CipherError => e
62
+ raise DecodingError, e.message
63
+ end
77
64
 
78
- def digest
79
- Digest::SHA1.hexdigest(@secret)[0..31]
65
+ def encrypt(text)
66
+ raise 'Text can\'t be nil' if text.nil?
67
+ if @secret.empty?
68
+ text
69
+ else
70
+ cpr = cipher
71
+ cpr.encrypt
72
+ cpr.key = digest
73
+ salt = SecureRandom.base64(Random.rand(8..32))
74
+ encrypted = cpr.update(salt + ' ' + text)
75
+ encrypted << cpr.final
76
+ Base58.binary_to_base58(encrypted)
80
77
  end
78
+ end
81
79
 
82
- def cipher
83
- OpenSSL::Cipher.new('aes-256-cbc')
84
- end
80
+ def digest
81
+ Digest::SHA1.hexdigest(@secret)[0..31]
82
+ end
83
+
84
+ def cipher
85
+ OpenSSL::Cipher.new('aes-256-cbc')
85
86
  end
86
87
  end
@@ -51,14 +51,14 @@ module GLogin
51
51
  # Returns a hash with two elements: login and avatar.
52
52
  # If the secret is empty, the text will be returned, without
53
53
  # any decryption. If the data is not valid, an exception
54
- # OpenSSL::Cipher::CipherError will be raised, which you have
54
+ # GLogin::Codec::DecodingError will be raised, which you have
55
55
  # to catch in your applicaiton and ignore the login attempt.
56
56
  def to_user
57
57
  plain = Codec.new(@secret).decrypt(@text)
58
58
  login, avatar, bearer, ctx = plain.split(GLogin::SPLIT, 4)
59
59
  if !@secret.empty? && ctx.to_s != @context
60
60
  raise(
61
- OpenSSL::Cipher::CipherError,
61
+ GLogin::Codec::DecodingError,
62
62
  "Context '#{@context}' expected, but '#{ctx}' found"
63
63
  )
64
64
  end
@@ -26,5 +26,5 @@
26
26
  # Copyright:: Copyright (c) 2017-2019 Yegor Bugayenko
27
27
  # License:: MIT
28
28
  module GLogin
29
- VERSION = '0.5.1'
29
+ VERSION = '0.6.0'
30
30
  end
@@ -0,0 +1,19 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <svg width="314px" height="314px" viewBox="0 0 314 314" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
3
+ <!-- Generator: Sketch 43.2 (39069) - http://www.bohemiancoding.com/sketch -->
4
+ <title>Group 2</title>
5
+ <desc>Created with Sketch.</desc>
6
+ <defs></defs>
7
+ <g id="Page-1" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
8
+ <g id="Group-2">
9
+ <g id="Group">
10
+ <circle id="Oval" fill="#053C5E" cx="157" cy="157" r="157"></circle>
11
+ <rect id="Rectangle" fill="#2B2B2B" x="0" y="234" width="233" height="68"></rect>
12
+ <text id="glogin" font-family="CourierNewPS-BoldMT, Courier New" font-size="50" font-weight="bold" fill="#FFFFFF">
13
+ <tspan x="28" y="282">glogin</tspan>
14
+ </text>
15
+ <path d="M135.676765,111.215287 C135.211714,113.540467 132.770932,120.688823 130.252813,127.100603 C127.734621,133.512383 125.2898,140.431445 124.819828,142.476214 C123.6868,147.405663 121.517836,147.185477 119.69487,141.95608 C116.899865,133.938362 105.171362,111.043501 103.859129,111.043501 C100.420089,111.043501 101.508757,114.986149 109.1771,130.30227 C113.739327,139.414532 118.032086,147.448041 118.716515,148.15443 C120.694817,150.196115 120.172626,154.319657 117.358599,158.877992 C113.567467,165.019055 107.570942,183.721189 108.054206,187.896804 C108.804956,194.383204 112.436054,192.210274 113.355359,184.724515 C114.48376,175.535577 120.945556,159.641154 124.897459,156.333653 C127.109021,154.482778 128.268636,151.905536 128.573284,148.163978 C128.818001,145.157747 131.906711,135.560972 135.437115,126.837819 C138.96752,118.114666 141.511492,110.079835 141.090434,108.982574 C139.755727,105.504315 136.554941,106.824333 135.676765,111.215287 M174.714659,126.841564 C169.37392,129.699438 169.197653,130.049915 167.452978,141.283475 C165.577352,153.360398 165.225112,168.513913 166.548289,180.203784 L167.566817,189.202501 L161.346213,191.9541 C154.601949,194.937416 150.91195,200.302465 153.156708,203.861146 C155.104824,206.949562 157.09025,206.70198 157.571826,203.310532 C158.083734,199.706169 166.097413,193.204052 170.29418,192.987978 C172.116999,192.894116 173.522874,191.717021 173.97823,189.903529 C174.383277,188.290613 177.740353,184.944554 181.438431,182.467858 C188.546172,177.707551 194.317883,170.450278 194.317883,166.273341 C194.317883,162.684548 189.493165,158.361603 185.487795,158.361603 C180.701636,158.361603 180.027195,155.239403 183.731222,150.229385 C188.308065,144.038966 188.284856,130.867646 183.690534,127.147387 C179.896537,124.075276 179.885006,124.074836 174.714659,126.841564 M182.67318,134.240439 C184.752542,140.153164 174.808961,161.185104 171.588639,157.68562 C169.868715,155.81653 172.863122,135.012708 175.266374,132.134564 C177.872553,129.013319 181.163896,129.949075 182.67318,134.240439 M184.854262,162.269586 C190.589839,163.855621 189.673839,167.881335 181.645765,176.371155 C177.281618,180.986262 173.904933,183.573859 173.371946,182.711474 C172.876636,181.91012 172.127869,176.711937 171.707987,171.159972 L170.944531,161.065463 L176.209475,161.200234 C179.105172,161.274339 182.995308,161.755548 184.854262,162.269586 M132.642184,172.492256 C131.593837,175.224098 133.152917,177.288844 136.264027,177.288844 C139.014965,177.288844 139.431396,176.221769 138.067531,172.667568 C137.039234,169.987871 133.645289,169.878218 132.642184,172.492256 M203.781503,185.840798 C203.781503,187.657595 209.264724,193.512152 210.966361,193.512152 C212.534035,193.512152 211.991059,187.391138 210.270768,185.670847 C208.092403,183.492409 203.781503,183.605293 203.781503,185.840798" id="Fill-6" fill="#427B8A" fill-rule="nonzero"></path>
16
+ </g>
17
+ </g>
18
+ </g>
19
+ </svg>
@@ -29,13 +29,11 @@ class TestAuth < Minitest::Test
29
29
  def test_authenticate_via_https
30
30
  auth = GLogin::Auth.new('1234', '4433', 'https://example.org')
31
31
  stub_request(:post, 'https://github.com/login/oauth/access_token').to_return(
32
- status: 200,
33
32
  body: {
34
33
  access_token: 'some-token'
35
34
  }.to_json
36
35
  )
37
36
  stub_request(:get, 'https://api.github.com/user').to_return(
38
- status: 200,
39
37
  body: {
40
38
  auth_code: '437849732894732',
41
39
  login: 'yegor256'
@@ -22,6 +22,7 @@
22
22
  # SOFTWARE.
23
23
 
24
24
  require 'minitest/autorun'
25
+ require 'base64'
25
26
  require_relative '../../lib/glogin/codec'
26
27
 
27
28
  class TestCodec < Minitest::Test
@@ -40,7 +41,7 @@ class TestCodec < Minitest::Test
40
41
  end
41
42
 
42
43
  def test_decrypts_with_invalid_password
43
- assert_raises OpenSSL::Cipher::CipherError do
44
+ assert_raises GLogin::Codec::DecodingError do
44
45
  GLogin::Codec.new('the wrong key').decrypt(
45
46
  GLogin::Codec.new('the right key').encrypt('the text')
46
47
  )
@@ -49,12 +50,12 @@ class TestCodec < Minitest::Test
49
50
 
50
51
  def test_encrypts_into_plain_string
51
52
  text = GLogin::Codec.new('6hFGrte5LLmwi').encrypt("K&j\n\n\tuIpwp00{]=")
52
- assert(text =~ %r{^[a-zA-Z0-9/=+]+$}, text)
53
+ assert(text =~ /^[a-zA-Z0-9]+$/, text)
53
54
  assert(!text.include?("\n"), text)
54
55
  end
55
56
 
56
57
  def test_decrypts_broken_text
57
- assert_raises OpenSSL::Cipher::CipherError do
58
+ assert_raises GLogin::Codec::DecodingError do
58
59
  GLogin::Codec.new('the key').decrypt('этот текст не был зашифрован')
59
60
  end
60
61
  end
@@ -74,7 +74,7 @@ class TestCookie < Minitest::Test
74
74
  end
75
75
 
76
76
  def test_fails_on_broken_text
77
- assert_raises OpenSSL::Cipher::CipherError do
77
+ assert_raises GLogin::Codec::DecodingError do
78
78
  GLogin::Cookie::Closed.new(
79
79
  GLogin::Cookie::Open.new(
80
80
  JSON.parse('{"login":"x","avatar_url":"x"}'),
@@ -87,7 +87,7 @@ class TestCookie < Minitest::Test
87
87
 
88
88
  def test_fails_on_wrong_context
89
89
  secret = 'fdjruewoijs789fdsufds89f7ds89fs'
90
- assert_raises OpenSSL::Cipher::CipherError do
90
+ assert_raises GLogin::Codec::DecodingError do
91
91
  GLogin::Cookie::Closed.new(
92
92
  GLogin::Cookie::Open.new(
93
93
  JSON.parse('{"login":"x","avatar_url":"x"}'),
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: glogin
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-05-09 00:00:00.000000000 Z
11
+ date: 2019-07-08 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: base58
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 0.2.3
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 0.2.3
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: codecov
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -148,6 +162,7 @@ files:
148
162
  - lib/glogin/codec.rb
149
163
  - lib/glogin/cookie.rb
150
164
  - lib/glogin/version.rb
165
+ - logo.svg
151
166
  - test/glogin/test_auth.rb
152
167
  - test/glogin/test_codec.rb
153
168
  - test/glogin/test_cookie.rb