github-api-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/.gitignore +22 -0
- data/.rspec +3 -0
- data/Gemfile +10 -0
- data/LICENSE.txt +22 -0
- data/README.md +39 -0
- data/Rakefile +2 -0
- data/github-api-auth.gemspec +26 -0
- data/lib/github/api/auth.rb +10 -0
- data/lib/github/api/auth/auth.rb +69 -0
- data/lib/github/api/auth/version.rb +7 -0
- data/spec/github/api/auth/auth_spec.rb +115 -0
- data/spec/spec_helper.rb +3 -0
- metadata +115 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ec1a20be8f8c517e329620ff1bdb7125ecb05f9b
|
4
|
+
data.tar.gz: 55c52ac500e87dd78e3f81d5b2d1c121c79baf13
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ab6746507501209a59fc87ff9347b3dfcbb85339a1fa64da16e4c77d136c06a2182f7aad03642cc561f2d67cb7ce6ce713bd78729de005f108ba15db0e267862
|
7
|
+
data.tar.gz: b65578d6f7a1b50e690223deabdea4fea975e1a5c313b4755731a1c21967490dd19611ae1396a305d3c23f353c9669a4e0d5b8dc556cf4aeefc23a04f700747d
|
data/.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
data/.rspec
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
# Specify your gem's dependencies in github-api-auth.gemspec
|
4
|
+
gemspec
|
5
|
+
|
6
|
+
# use all bleeding edge of rspec
|
7
|
+
%w(core mocks expectations support rails legacy_formatters collection_matchers).each do |part|
|
8
|
+
gem "rspec-#{part}", github: "rspec/rspec-#{part}"
|
9
|
+
end
|
10
|
+
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Alexey Fedorov
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# Github::Api::Auth
|
2
|
+
|
3
|
+
Usefull class to authenticate to github api just once and get authenticated Octokit::Client in return. OTP included.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'github-api-auth'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install github-api-auth
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
octo_client = Github::Api::Auth.new.github
|
22
|
+
|
23
|
+
And you are good to go. It will ask you for login and password and store newly created access token in local file. If OTP is required it will initiate code send process and will ask for code.
|
24
|
+
|
25
|
+
## Changelog
|
26
|
+
|
27
|
+
0.1.0
|
28
|
+
|
29
|
+
- Added basic authentication
|
30
|
+
- Added OTP authentication
|
31
|
+
- Added authenticaton by stored access token
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
1. Fork it ( https://github.com/alex-fedorov/github-api-auth/fork )
|
36
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
37
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
38
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
39
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'github/api/auth/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "github-api-auth"
|
8
|
+
spec.version = Github::Api::Auth::VERSION
|
9
|
+
spec.authors = ["Alexey Fedorov"]
|
10
|
+
spec.email = ["alexey.fedorov@wimdu.com"]
|
11
|
+
spec.summary = %q{Usefull class to authenticate to github api just once and get authenticated Octokit::Client in return. OTP included.}
|
12
|
+
spec.description = %q{}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
|
24
|
+
spec.add_runtime_dependency "octokit"
|
25
|
+
spec.add_runtime_dependency "highline"
|
26
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
require 'octokit'
|
3
|
+
require 'highline/import'
|
4
|
+
|
5
|
+
class Github::Api::Auth::Klass; include Github::Api::Auth end
|
6
|
+
|
7
|
+
module Github::Api::Auth
|
8
|
+
AUTH_FILE = "#{ENV['HOME']}/.github_token"
|
9
|
+
AUTH_LOCAL_FILE = ".github_token"
|
10
|
+
SCOPES = "repo:status"
|
11
|
+
|
12
|
+
attr_accessor :github
|
13
|
+
|
14
|
+
def self.new
|
15
|
+
Github::Api::Auth::Klass.new
|
16
|
+
end
|
17
|
+
|
18
|
+
def initialize
|
19
|
+
authenticate
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def authenticate
|
25
|
+
configure_from_file || basic_authentication
|
26
|
+
end
|
27
|
+
|
28
|
+
def basic_authentication
|
29
|
+
login = ask('github username: ')
|
30
|
+
password = ask('password: ') { |c| c.echo = false }
|
31
|
+
self.github = Octokit::Client.new(login: login, password: password)
|
32
|
+
token = create_access_token
|
33
|
+
store_token(sanitize_token(token))
|
34
|
+
configure_from_file
|
35
|
+
end
|
36
|
+
|
37
|
+
def configure_from_file
|
38
|
+
path = File.exists?(AUTH_LOCAL_FILE) ? AUTH_LOCAL_FILE : AUTH_FILE
|
39
|
+
config = YAML.load_file(path)
|
40
|
+
self.github = Octokit::Client.new(access_token: config[:token])
|
41
|
+
true
|
42
|
+
rescue
|
43
|
+
false
|
44
|
+
end
|
45
|
+
|
46
|
+
def create_access_token
|
47
|
+
scopes = SCOPES
|
48
|
+
note = gen_token_note
|
49
|
+
github.create_authorization(scopes: scopes, note: note)
|
50
|
+
rescue Octokit::OneTimePasswordRequired => e
|
51
|
+
otp = ask('one time password required: ')
|
52
|
+
github.create_authorization(scopes: scopes, note: note, headers: { 'X-GitHub-OTP' => otp })
|
53
|
+
end
|
54
|
+
|
55
|
+
def store_token(token)
|
56
|
+
File.open(AUTH_LOCAL_FILE, 'w') { |f| f.write(token.to_yaml) }
|
57
|
+
end
|
58
|
+
|
59
|
+
def sanitize_token(token)
|
60
|
+
token.to_h.reject { |k, v| k == :app }
|
61
|
+
end
|
62
|
+
|
63
|
+
def gen_token_note
|
64
|
+
# TODO Make this use existing token instead of creating new one
|
65
|
+
# NOTE We need this number if user want to authenticate multiple devices
|
66
|
+
number = Time.now.to_i
|
67
|
+
"Wimdu pre ci notifications #{number}"
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,115 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
require 'octokit'
|
3
|
+
require 'github/api/auth'
|
4
|
+
|
5
|
+
RSpec.describe Github::Api::Auth do
|
6
|
+
describe '.new and #authenticate' do
|
7
|
+
context 'having file .github_token in place with valid token' do
|
8
|
+
let(:valid_token) { '123456' }
|
9
|
+
let(:client) { double('Octokit::Client') }
|
10
|
+
|
11
|
+
it 'successfully authenticates without asking user for anything' do
|
12
|
+
allow(File).to receive(:exists?).with('.github_token') { true }
|
13
|
+
allow(YAML).to receive(:load_file).
|
14
|
+
with('.github_token') { { token: valid_token } }
|
15
|
+
allow(Octokit::Client).to receive(:new).
|
16
|
+
with(access_token: valid_token) { client }
|
17
|
+
expect_any_instance_of(subject).not_to receive(:basic_authentication)
|
18
|
+
github = subject.new
|
19
|
+
expect(github.github).to eq(client)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
context 'having file .github_token in place with invalid token' do
|
24
|
+
let(:invalid_token) { '654321' }
|
25
|
+
|
26
|
+
it 'falls back to basic authentication' do
|
27
|
+
allow(File).to receive(:exists?).with('.github_token') { true }
|
28
|
+
allow(YAML).to receive(:load_file).
|
29
|
+
with('.github_token') { { token: invalid_token } }
|
30
|
+
allow(Octokit::Client).to receive(:new).
|
31
|
+
with(access_token: invalid_token).and_raise(RuntimeError.new)
|
32
|
+
expect_any_instance_of(subject).to receive(:basic_authentication) { true }
|
33
|
+
github = subject.new
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
context 'without file .github_token, but having ~/.github_token' do
|
38
|
+
let(:valid_token) { '123456' }
|
39
|
+
let(:client) { double('Octokit::Client') }
|
40
|
+
let(:path) { "#{ENV['HOME']}/.github_token" }
|
41
|
+
|
42
|
+
it 'successfully authenticates without asking user for anything' do
|
43
|
+
allow(File).to receive(:exists?).with('.github_token') { false }
|
44
|
+
allow(File).to receive(:exists?).with(path) { true }
|
45
|
+
allow(YAML).to receive(:load_file).
|
46
|
+
with(path) { { token: valid_token } }
|
47
|
+
allow(Octokit::Client).to receive(:new).
|
48
|
+
with(access_token: valid_token) { client }
|
49
|
+
expect_any_instance_of(subject).not_to receive(:basic_authentication)
|
50
|
+
github = subject.new
|
51
|
+
expect(github.github).to eq(client)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
describe '#basic_authentication' do
|
57
|
+
let(:username) { 'john' }
|
58
|
+
let(:password) { 'super strong password' }
|
59
|
+
let(:client) { double('Octokit::Client') }
|
60
|
+
let(:token) { '123456' }
|
61
|
+
let(:authorization) { { scopes: 'repo:status', note: 'wimdu note' } }
|
62
|
+
let(:otp) { '987654' }
|
63
|
+
|
64
|
+
before do
|
65
|
+
allow(Octokit::Client).to receive(:new).
|
66
|
+
with(login: username, password: password) { client }
|
67
|
+
end
|
68
|
+
|
69
|
+
context 'having #configure_from_file failed' do
|
70
|
+
before do
|
71
|
+
allow_any_instance_of(subject).to receive(:configure_from_file) { false }
|
72
|
+
allow_any_instance_of(subject).to receive(:gen_token_note) { 'wimdu note' }
|
73
|
+
allow_any_instance_of(subject).to receive(:ask).
|
74
|
+
with('github username: ') { username }
|
75
|
+
allow_any_instance_of(subject).to receive(:ask).
|
76
|
+
with('password: ') { password }
|
77
|
+
end
|
78
|
+
|
79
|
+
it 'gets called' do
|
80
|
+
expect_any_instance_of(subject).to receive(:basic_authentication) { true }
|
81
|
+
github = subject.new
|
82
|
+
end
|
83
|
+
|
84
|
+
it 'asks user for username and password and creates access token' do
|
85
|
+
expect_any_instance_of(subject).to receive(:ask).
|
86
|
+
with('github username: ') { username }
|
87
|
+
expect_any_instance_of(subject).to receive(:ask).
|
88
|
+
with('password: ') { password }
|
89
|
+
expect_any_instance_of(subject).to receive(:create_access_token) do
|
90
|
+
{ token: token }
|
91
|
+
end
|
92
|
+
expect_any_instance_of(subject).to receive(:store_token).
|
93
|
+
with({ token: token })
|
94
|
+
github = subject.new
|
95
|
+
end
|
96
|
+
|
97
|
+
context 'having one time password required' do
|
98
|
+
it 'asks user for otp' do
|
99
|
+
expect(client).to receive(:create_authorization).
|
100
|
+
with(authorization).and_raise(Octokit::OneTimePasswordRequired.new)
|
101
|
+
expect_any_instance_of(subject).to receive(:ask).
|
102
|
+
with('one time password required: ') { otp }
|
103
|
+
expect(client).to receive(:create_authorization).
|
104
|
+
with(authorization.merge(headers: { 'X-GitHub-OTP' => otp })) do
|
105
|
+
{ token: token }
|
106
|
+
end
|
107
|
+
expect_any_instance_of(subject).to receive(:store_token).
|
108
|
+
with({ token: token })
|
109
|
+
github = subject.new
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: github-api-auth
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Alexey Fedorov
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-07-02 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.6'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
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: octokit
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: highline
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: ''
|
70
|
+
email:
|
71
|
+
- alexey.fedorov@wimdu.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- Gemfile
|
79
|
+
- LICENSE.txt
|
80
|
+
- README.md
|
81
|
+
- Rakefile
|
82
|
+
- github-api-auth.gemspec
|
83
|
+
- lib/github/api/auth.rb
|
84
|
+
- lib/github/api/auth/auth.rb
|
85
|
+
- lib/github/api/auth/version.rb
|
86
|
+
- spec/github/api/auth/auth_spec.rb
|
87
|
+
- spec/spec_helper.rb
|
88
|
+
homepage: ''
|
89
|
+
licenses:
|
90
|
+
- MIT
|
91
|
+
metadata: {}
|
92
|
+
post_install_message:
|
93
|
+
rdoc_options: []
|
94
|
+
require_paths:
|
95
|
+
- lib
|
96
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - ">="
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
requirements: []
|
107
|
+
rubyforge_project:
|
108
|
+
rubygems_version: 2.2.2
|
109
|
+
signing_key:
|
110
|
+
specification_version: 4
|
111
|
+
summary: Usefull class to authenticate to github api just once and get authenticated
|
112
|
+
Octokit::Client in return. OTP included.
|
113
|
+
test_files:
|
114
|
+
- spec/github/api/auth/auth_spec.rb
|
115
|
+
- spec/spec_helper.rb
|