Skip to content

Commit 1140394

Browse files
committed
Merge branch 'add-github-username-to-key'
* add-github-username-to-key: Add github profile link as comment on ssh key
2 parents 665812e + 5db0dad commit 1140394

File tree

9 files changed

+72
-40
lines changed

9 files changed

+72
-40
lines changed

lib/github/auth.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require 'github/auth/version'
2+
require 'github/auth/key'
23
require 'github/auth/keys_client'
34
require 'github/auth/keys_file'
45
require 'github/auth/cli'

lib/github/auth/key.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module Github::Auth
2+
# Represents a username/key pair from GitHub
3+
Key = Struct.new(:username, :key) do
4+
def to_a
5+
[self]
6+
end
7+
8+
def to_s
9+
"#{key} #{url}"
10+
end
11+
12+
def url
13+
"https://github.com/#{username}"
14+
end
15+
end
16+
end

lib/github/auth/keys_client.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ def initialize(options = {})
2626
end
2727

2828
def keys
29-
@keys ||= Array(github_response).map { |entry| entry.fetch 'key' }
29+
@keys ||= Array(github_response).map do |entry|
30+
Github::Auth::Key.new username, entry.fetch('key')
31+
end
3032
end
3133

3234
private

lib/github/auth/keys_file.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def initialize(options = {})
1414

1515
def write!(keys)
1616
Array(keys).each do |key|
17-
unless keys_file_content.include? key
17+
unless keys_file_content.include? key.key
1818
append_keys_file do |keys_file|
1919
keys_file.write "\n" unless keys_file_content.empty?
2020
keys_file.write key
@@ -53,7 +53,10 @@ def with_keys_file(mode, block)
5353

5454
def keys_file_content_without(keys)
5555
keys_file_content.tap do |content|
56-
Array(keys).each {|k| content.gsub! /#{Regexp.escape k}( .*)?$\n?/, '' }
56+
Array(keys).each do |key|
57+
content.gsub! /#{Regexp.escape key.key}( .*)?$\n?/, ''
58+
end
59+
5760
content.strip!
5861
end
5962
end

spec/acceptance/github/auth/cli_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ def cli(argv)
2222
it 'adds and removes keys from the keys file' do
2323
cli(%w(add chrishunt)).execute
2424

25-
keys_file.read.tap do |content|
26-
keys.each { |key| expect(content).to include key }
25+
keys_file.read.tap do |keys_file_content|
26+
keys.each { |key| expect(keys_file_content).to include key.to_s }
2727
end
2828

2929
cli(%w(remove chrishunt)).execute

spec/acceptance/github/auth/keys_file_spec.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66
it 'writes and deletes keys from the keys file' do
77
tempfile = Tempfile.new 'authorized_keys'
88
keys_file = described_class.new path: tempfile.path
9-
keys = %w(abc123 def456)
9+
10+
keys = [
11+
Github::Auth::Key.new('chris', 'abc123'),
12+
Github::Auth::Key.new('doug', 'def456')
13+
]
1014

1115
keys_file.write! keys
1216
expect(tempfile.read).to include keys.join("\n")
@@ -15,8 +19,8 @@
1519

1620
keys_file.delete! keys.first
1721
tempfile.read.tap do |tempfile_content|
18-
expect(tempfile_content).to_not include keys.first
19-
expect(tempfile_content).to include keys.last
22+
expect(tempfile_content).to_not include keys.first.to_s
23+
expect(tempfile_content).to include keys.last.to_s
2024
end
2125

2226
tempfile.rewind

spec/support/mock_github_server.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@
22
require 'sinatra/base'
33
require 'json'
44

5+
require 'github/auth/key'
6+
57
module Github::Auth
68
class MockGithubServer < Sinatra::Base
7-
KEYS = %w(abc234 def456)
9+
KEYS = [
10+
Github::Auth::Key.new('chrishunt', 'abc123'),
11+
Github::Auth::Key.new('chrishunt', 'def456')
12+
]
813

914
set :port, 8001
1015

@@ -16,8 +21,8 @@ class MockGithubServer < Sinatra::Base
1621
content_type :json
1722

1823
[
19-
{ 'id' => 123, 'key' => KEYS[0] },
20-
{ 'id' => 456, 'key' => KEYS[1] }
24+
{ 'id' => 123, 'key' => KEYS[0].key },
25+
{ 'id' => 456, 'key' => KEYS[1].key }
2126
].to_json
2227
end
2328
end

spec/unit/github/auth/keys_client_spec.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require 'spec_helper'
2+
require 'github/auth/key'
23
require 'github/auth/keys_client'
34

45
describe Github::Auth::KeysClient do
@@ -50,12 +51,15 @@
5051

5152
context 'when the github user has keys' do
5253
let(:parsed_response) {[
53-
{ 'id' => 123, 'key' => 'BLAHBLAH' },
54-
{ 'id' => 456, 'key' => 'FLARBBLU' }
54+
{ 'id' => 123, 'key' => 'abc123' },
55+
{ 'id' => 456, 'key' => 'def456' }
5556
]}
5657

5758
it 'returns the keys' do
58-
expected_keys = parsed_response.map { |entry| entry.fetch 'key' }
59+
expected_keys = parsed_response.map do |entry|
60+
Github::Auth::Key.new username, entry.fetch('key')
61+
end
62+
5963
expect(subject.keys).to eq expected_keys
6064
end
6165
end

spec/unit/github/auth/keys_file_spec.rb

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
require 'spec_helper'
22
require 'tempfile'
3+
require 'github/auth/key'
34
require 'github/auth/keys_file'
45

56
describe Github::Auth::KeysFile do
@@ -38,11 +39,13 @@
3839

3940
describe '#write!' do
4041
shared_examples_for 'a successful key addition' do
41-
it 'writes the key to the keys file' do
42+
it 'writes the key and github url to the keys file' do
4243
subject.write! keys
4344

4445
keys_file.read.tap do |keys_file_content|
45-
keys.each { |key| expect(keys_file_content).to include key }
46+
keys.each do |key|
47+
expect(keys_file_content).to include "#{key.key} #{key.url}"
48+
end
4649
end
4750
end
4851

@@ -56,20 +59,24 @@
5659
end
5760

5861
context 'with many keys' do
59-
let(:keys) { %w(abc123 def456 ghi789) }
62+
let(:keys) {[
63+
Github::Auth::Key.new('chris', 'abc123'),
64+
Github::Auth::Key.new('chris', 'def456'),
65+
Github::Auth::Key.new('doug', 'ghi789')
66+
]}
6067

6168
it_should_behave_like 'a successful key addition'
6269
end
6370

6471
context 'with a single key' do
65-
let(:keys) { %w(abc123) }
72+
let(:keys) {[ Github::Auth::Key.new('chris', 'abc123') ]}
6673

6774
it_should_behave_like 'a successful key addition'
6875
end
6976

7077
context 'with existing keys in the keys file' do
7178
let(:existing_keys) { %w(abc123 def456 ghi789) }
72-
let(:keys) { %w(jkl012) }
79+
let(:keys) {[ Github::Auth::Key.new('chris', 'jkl012') ]}
7380

7481
before do
7582
keys_file.write existing_keys.join("\n")
@@ -89,7 +96,7 @@
8996
end
9097

9198
it 'does not write duplicate keys into the keys file' do
92-
subject.write! existing_keys.first
99+
subject.write! Github::Auth::Key.new('chris', existing_keys.first)
93100

94101
expect(keys_file.readlines.count).to eq existing_keys.count
95102
end
@@ -100,7 +107,7 @@
100107

101108
it 'raises PermissionDeniedError' do
102109
expect {
103-
subject.write! %w(abc123 def456)
110+
subject.write! Github::Auth::Key.new('chris', 'abc123')
104111
}.to raise_error Github::Auth::KeysFile::PermissionDeniedError
105112
end
106113
end
@@ -117,7 +124,11 @@
117124
end
118125

119126
describe '#delete!' do
120-
let(:keys) { %w(abc123 def456 ghi789) }
127+
let(:keys) {[
128+
Github::Auth::Key.new('chris', 'abc123'),
129+
Github::Auth::Key.new('chris', 'def456'),
130+
Github::Auth::Key.new('doug', 'ghi789')
131+
]}
121132

122133
before do
123134
keys_file.write keys.join("\n")
@@ -128,15 +139,15 @@
128139
it 'removes the key from the keys file' do
129140
subject.delete! key
130141

131-
expect(keys_file.read).to_not include key
142+
expect(keys_file.read).to_not include key.to_s
132143
end
133144

134145
it 'does not remove the other keys from the keys file' do
135146
subject.delete! key
136147

137148
keys_file.read.tap do |keys_file_content|
138-
keys.reject { |other_key| other_key =~ /#{key}/ }.each do |key|
139-
expect(keys_file_content).to include key
149+
keys.reject { |other_key| other_key == key }.each do |key|
150+
expect(keys_file_content).to include key.to_s
140151
end
141152
end
142153
end
@@ -168,22 +179,8 @@
168179
it_should_behave_like 'a successful key removal'
169180
end
170181

171-
context 'when the key has a comment' do
172-
let(:keys) {[ 'abc123', "#{key} #{comment}", 'ghi789' ]}
173-
let(:key) { 'def456' }
174-
let(:comment) { 'this is a comment' }
175-
176-
it_should_behave_like 'a successful key removal'
177-
178-
it 'removes the comment from the keys file' do
179-
subject.delete! key
180-
181-
expect(keys_file.read).to_not include comment
182-
end
183-
end
184-
185182
context 'when the keys file does not have the key' do
186-
let(:key) { 'not-in-the-keys-file' }
183+
let(:key) { Github::Auth::Key.new('sallie', 'not-in-the-keys-file') }
187184

188185
it 'does not modify the keys file' do
189186
keys_file.read.tap do |original_keys_file_content|

0 commit comments

Comments
 (0)