Skip to content

Commit d854c15

Browse files
committed
Build v6.0.0
1 parent 17f0f3d commit d854c15

File tree

2 files changed

+129
-35
lines changed

2 files changed

+129
-35
lines changed

build/gist

Lines changed: 81 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1318,7 +1318,7 @@ end
13181318
module Gist
13191319
extend self
13201320

1321-
VERSION = '5.1.0'
1321+
VERSION = '6.0.0'
13221322

13231323
# A list of clipboard commands with copy and paste support.
13241324
CLIPBOARD_COMMANDS = {
@@ -1329,12 +1329,16 @@ module Gist
13291329
}
13301330

13311331
GITHUB_API_URL = URI("https://api.github.com/")
1332+
GITHUB_URL = URI("https://github.com/")
13321333
GIT_IO_URL = URI("https://git.io")
13331334

13341335
GITHUB_BASE_PATH = ""
13351336
GHE_BASE_PATH = "/api/v3"
13361337

1338+
GITHUB_CLIENT_ID = '4f7ec0d4eab38e74384e'
1339+
13371340
URL_ENV_NAME = "GITHUB_URL"
1341+
CLIENT_ID_ENV_NAME = "GIST_CLIENT_ID"
13381342

13391343
USER_AGENT = "gist/#{VERSION} (Net::HTTP, #{RUBY_DESCRIPTION})"
13401344

@@ -1350,7 +1354,7 @@ module Gist
13501354
module AuthTokenFile
13511355
def self.filename
13521356
if ENV.key?(URL_ENV_NAME)
1353-
File.expand_path "~/.gist.#{ENV[URL_ENV_NAME].gsub(/:/, '.').gsub(/[^a-z0-9.]/, '')}"
1357+
File.expand_path "~/.gist.#{ENV[URL_ENV_NAME].gsub(/:/, '.').gsub(/[^a-z0-9.-]/, '')}"
13541358
else
13551359
File.expand_path "~/.gist"
13561360
end
@@ -1506,19 +1510,12 @@ module Gist
15061510
url = "#{base_path}"
15071511

15081512
if user == ""
1509-
access_token = auth_token()
1510-
if access_token.to_s != ''
1511-
url << "/gists?per_page=100"
1512-
get_gist_pages(url, access_token)
1513-
else
1514-
raise Error, "Not authenticated. Use 'gist --login' to login or 'gist -l username' to view public gists."
1515-
end
1516-
1513+
url << "/gists?per_page=100"
15171514
else
15181515
url << "/users/#{user}/gists?per_page=100"
1519-
get_gist_pages(url)
15201516
end
15211517

1518+
get_gist_pages(url, auth_token())
15221519
end
15231520

15241521
def read_gist(id, file_name=nil)
@@ -1642,15 +1639,71 @@ module Gist
16421639

16431640
# Log the user into gist.
16441641
#
1642+
def login!(credentials={})
1643+
if (login_url == GITHUB_URL || ENV.key?(CLIENT_ID_ENV_NAME)) && credentials.empty? && !ENV.key?('GIST_USE_USERNAME_AND_PASSWORD')
1644+
device_flow_login!
1645+
else
1646+
access_token_login!(credentials)
1647+
end
1648+
end
1649+
1650+
def device_flow_login!
1651+
puts "Requesting login parameters..."
1652+
request = Net::HTTP::Post.new("/login/device/code")
1653+
request.body = JSON.dump({
1654+
:scope => 'gist',
1655+
:client_id => client_id,
1656+
})
1657+
request.content_type = 'application/json'
1658+
request['accept'] = "application/json"
1659+
response = http(login_url, request)
1660+
1661+
if response.code != '200'
1662+
raise Error, "HTTP #{response.code}: #{response.body}"
1663+
end
1664+
1665+
body = JSON.parse(response.body)
1666+
1667+
puts "Please sign in at #{body['verification_uri']}"
1668+
puts " and enter code: #{body['user_code']}"
1669+
device_code = body['device_code']
1670+
interval = body['interval']
1671+
1672+
loop do
1673+
sleep(interval.to_i)
1674+
request = Net::HTTP::Post.new("/login/oauth/access_token")
1675+
request.body = JSON.dump({
1676+
:client_id => client_id,
1677+
:grant_type => 'urn:ietf:params:oauth:grant-type:device_code',
1678+
:device_code => device_code
1679+
})
1680+
request.content_type = 'application/json'
1681+
request['Accept'] = 'application/json'
1682+
response = http(login_url, request)
1683+
if response.code != '200'
1684+
raise Error, "HTTP #{response.code}: #{response.body}"
1685+
end
1686+
body = JSON.parse(response.body)
1687+
break unless body['error'] == 'authorization_pending'
1688+
end
1689+
1690+
if body['error']
1691+
raise Error, body['error_description']
1692+
end
1693+
1694+
AuthTokenFile.write JSON.parse(response.body)['access_token']
1695+
1696+
puts "Success! #{ENV[URL_ENV_NAME] || "https://github.com/"}settings/connections/applications/#{client_id}"
1697+
end
1698+
1699+
# Logs the user into gist.
1700+
#
16451701
# This method asks the user for a username and password, and tries to obtain
16461702
# and OAuth2 access token, which is then stored in ~/.gist
16471703
#
16481704
# @raise [Gist::Error] if something went wrong
1649-
# @param [Hash] credentials login details
1650-
# @option credentials [String] :username
1651-
# @option credentials [String] :password
16521705
# @see http://developer.github.com/v3/oauth/
1653-
def login!(credentials={})
1706+
def access_token_login!(credentials={})
16541707
puts "Obtaining OAuth2 access_token from GitHub."
16551708
loop do
16561709
print "GitHub username: "
@@ -1707,7 +1760,11 @@ module Gist
17071760
env = ENV['http_proxy'] || ENV['HTTP_PROXY']
17081761
connection = if env
17091762
proxy = URI(env)
1710-
Net::HTTP::Proxy(proxy.host, proxy.port).new(uri.host, uri.port)
1763+
if proxy.user
1764+
Net::HTTP::Proxy(proxy.host, proxy.port, proxy.user, proxy.password).new(uri.host, uri.port)
1765+
else
1766+
Net::HTTP::Proxy(proxy.host, proxy.port).new(uri.host, uri.port)
1767+
end
17111768
else
17121769
Net::HTTP.new(uri.host, uri.port)
17131770
end
@@ -1861,11 +1918,19 @@ Could not find copy command, tried:
18611918
ENV.key?(URL_ENV_NAME) ? GHE_BASE_PATH : GITHUB_BASE_PATH
18621919
end
18631920

1921+
def login_url
1922+
ENV.key?(URL_ENV_NAME) ? URI(ENV[URL_ENV_NAME]) : GITHUB_URL
1923+
end
1924+
18641925
# Get the API URL
18651926
def api_url
18661927
ENV.key?(URL_ENV_NAME) ? URI(ENV[URL_ENV_NAME]) : GITHUB_API_URL
18671928
end
18681929

1930+
def client_id
1931+
ENV.key?(CLIENT_ID_ENV_NAME) ? URI(ENV[CLIENT_ID_ENV_NAME]) : GITHUB_CLIENT_ID
1932+
end
1933+
18691934
def legacy_private_gister?
18701935
return unless which('git')
18711936
`git config --global gist.private` =~ /\Ayes|1|true|on\z/i

build/gist.1

Lines changed: 48 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
.\" generated with Ronn/v0.7.3
22
.\" http://github.com/rtomayko/ronn/tree/0.7.3
33
.
4-
.TH "GIST" "1" "May 2018" "" "Gist manual"
4+
.TH "GIST" "1" "August 2020" "" "Gist manual"
55
.
66
.SH "NAME"
77
\fBgist\fR \- upload code to https://gist\.github\.com
@@ -29,6 +29,12 @@ For OS X, gist lives in Homebrew
2929
.IP
3030
brew install gist
3131
.
32+
.IP "\(bu" 4
33+
For FreeBSD, gist lives in ports
34+
.
35+
.IP
36+
pkg install gist
37+
.
3238
.IP "" 0
3339
.
3440
.SH "Command"
@@ -121,14 +127,45 @@ See \fBgist \-\-help\fR for more detail\.
121127
.IP "" 0
122128
.
123129
.SH "Login"
124-
If you want to associate your gists with your GitHub account, you need to login with gist\. It doesn\'t store your username and password, it just uses them to get an OAuth2 token (with the "gist" permission)\.
130+
Before you use \fBgist\fR for the first time you will need to log in\. There are two supported login flows:
131+
.
132+
.IP "1." 4
133+
The Github device\-code Oauth flow\. This is the default for authenticating to github\.com, and can be enabled for Github Enterprise by creating an Oauth app, and exporting the environment variable \fBGIST_CLIENT_ID\fR with the client id of the Oauth app\.
134+
.
135+
.IP "2." 4
136+
The (deprecated) username and password token exchange flow\. This is the default for GitHub Enterprise, and can be used to log into github\.com by exporting the environment variable \fBGIST_USE_USERNAME_AND_PASSWORD\fR\.
137+
.
138+
.IP "" 0
139+
.
140+
.SS "The device\-code flow"
141+
This flow allows you to obtain a token by logging into GitHub in the browser and typing a verification code\. This is the preferred mechanism\.
142+
.
143+
.IP "" 4
144+
.
145+
.nf
146+
147+
gist \-\-login
148+
Requesting login parameters\.\.\.
149+
Please sign in at https://github\.com/login/device
150+
and enter code: XXXX\-XXXX
151+
Success! https://github\.com/settings/connections/applications/4f7ec0d4eab38e74384e
152+
.
153+
.fi
154+
.
155+
.IP "" 0
156+
.
157+
.P
158+
The returned access_token is stored in \fB~/\.gist\fR and used for all future gisting\. If you need to you can revoke access from https://github\.com/settings/connections/applications/4f7ec0d4eab38e74384e\.
159+
.
160+
.SS "The username\-password flow"
161+
This flow asks for your GitHub username and password (and 2FA code), and exchanges them for a token with the "gist" permission (your username and password are not stored)\. This mechanism is deprecated by GitHub, but may still work with GitHub Enterprise\.
125162
.
126163
.IP "" 4
127164
.
128165
.nf
129166

130167
gist \-\-login
131-
Obtaining OAuth2 access_token from github\.
168+
Obtaining OAuth2 access_token from GitHub\.
132169
GitHub username: ConradIrwin
133170
GitHub password:
134171
2\-factor auth code:
@@ -141,19 +178,11 @@ Success! https://github\.com/settings/tokens
141178
.P
142179
This token is stored in \fB~/\.gist\fR and used for all future gisting\. If you need to you can revoke it from https://github\.com/settings/tokens, or just delete the file\.
143180
.
144-
.IP "\(bu" 4
145-
After you\'ve done this, you can still upload gists anonymously with \fB\-a\fR\.
146-
.
147-
.IP
148-
gist \-a a\.rb
149-
.
150-
.IP "" 0
151-
.
152181
.P
153-
If you have a complicated authorization requirement you can manually create a token file by pasting a Github token with only the \fBgist\fR permission into a file called \fB~/\.gist\fR\. You can create one from https://github\.com/settings/tokens
182+
If you have a complicated authorization requirement you can manually create a token file by pasting a GitHub token with \fBgist\fR scope (and maybe the \fBuser:email\fR for GitHub Enterprise) into a file called \fB~/\.gist\fR\. You can create one from https://github\.com/settings/tokens
154183
.
155184
.P
156-
This file should contain only the token (~40 hex characters), and to make it easier to edit, can optionally have a final newline (\en or \er\en)\.
185+
This file should contain only the token (~40 hex characters), and to make it easier to edit, can optionally have a final newline (\fB\en\fR or \fB\er\en\fR)\.
157186
.
158187
.P
159188
For example, one way to create this file would be to run:
@@ -162,12 +191,15 @@ For example, one way to create this file would be to run:
162191
.
163192
.nf
164193

165-
echo MY_SECRET_TOKEN > ~/\.gist
194+
(umask 0077 && echo MY_SECRET_TOKEN > ~/\.gist)
166195
.
167196
.fi
168197
.
169198
.IP "" 0
170199
.
200+
.P
201+
The \fBumask\fR ensures that the file is only accessible from your user account\.
202+
.
171203
.SS "GitHub Enterprise"
172204
If you\'d like \fBgist\fR to use your locally installed GitHub Enterprise \fIhttps://enterprise\.github\.com/\fR, you need to export the \fBGITHUB_URL\fR environment variable (usually done in your \fB~/\.bashrc\fR)\.
173205
.
@@ -182,7 +214,7 @@ export GITHUB_URL=http://github\.internal\.example\.com/
182214
.IP "" 0
183215
.
184216
.P
185-
Once you\'ve done this and restarted your terminal (or run \fBsource ~/\.bashrc\fR), gist will automatically use github enterprise instead of the public github\.com
217+
Once you\'ve done this and restarted your terminal (or run \fBsource ~/\.bashrc\fR), gist will automatically use GitHub Enterprise instead of the public github\.com
186218
.
187219
.P
188220
Your token for GitHub Enterprise will be stored in \fB\.gist\.<protocol>\.<server\.name>[\.<port>]\fR (e\.g\. \fB~/\.gist\.http\.github\.internal\.example\.com\fR for the GITHUB_URL example above) instead of \fB~/\.gist\fR\.
@@ -218,9 +250,6 @@ If you need more advanced features you can also pass:
218250
\fB:update\fR to update an existing gist (can be a URL or an id)\.
219251
.
220252
.IP "\(bu" 4
221-
\fB:anonymous\fR to submit an anonymous gist (default is false)\.
222-
.
223-
.IP "\(bu" 4
224253
\fB:copy\fR to copy the resulting URL to the clipboard (default is false)\.
225254
.
226255
.IP "\(bu" 4
@@ -229,7 +258,7 @@ If you need more advanced features you can also pass:
229258
.IP "" 0
230259
.
231260
.P
232-
NOTE: The access_token must have the "gist" scope\.
261+
NOTE: The access_token must have the \fBgist\fR scope and may also require the \fBuser:email\fR scope\.
233262
.
234263
.IP "\(bu" 4
235264
If you want to upload multiple files in the same gist, you can:

0 commit comments

Comments
 (0)