Skip to content

Commit 0897e73

Browse files
committed
Use Bearer auth for GitHub API
This is a transparent change to users. Bearer auth doesn't need the username, so it's ignored if users provide it.
1 parent ec37312 commit 0897e73

File tree

6 files changed

+55
-24
lines changed

6 files changed

+55
-24
lines changed

guides/advanced/customizing-systems.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,8 +347,8 @@ retrieve it in the `artifacts_sites` list of the `nerves_package` config.
347347
There are currently five different artifact site helpers:
348348

349349
- `{:github_releases, "organization/repo"}`
350-
- `{:github_api, "organization/repo", username: "", token: "", tag: ""}`
351-
- `{:gitea_releases, "site/organization/repo}`
350+
- `{:github_api, "organization/repo", token: "", tag: ""}`
351+
- `{:gitea_releases, "site/organization/repo"}`
352352
- `{:gitea_api, "organization/repo", base_url: "https://gitea.com/", token: "", tag: ""}`
353353
- `{:prefix, "url", opts \\ []}`
354354

guides/advanced/systems.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ The following keys are supported:
228228

229229
```elixir
230230
{:github_releases, "organization/project"}
231-
{:github_api, "organization/project", username: System.get_env("GITHUB_USER"), token: System.get_env("GITHUB_TOKEN"), tag: @version}
231+
{:github_api, "organization/project", token: System.get_env("GITHUB_TOKEN"), tag: @version}
232232
{:prefix, "http://myserver.com/artifacts"}
233233
{:prefix, "file:///my_artifacts/"}
234234
{:prefix, "/users/my_user/artifacts/"}
@@ -241,10 +241,10 @@ The following keys are supported:
241241

242242
For an artifact site that uses GitHub Releases in a private repo, [create a
243243
personal access token](https://help.github.com/en/articles/creating-a-personal-access-token-for-the-command-line)
244-
and use `:github_api` with `username`, `token`, and `tag` options:
244+
and use `:github_api` with `token` and `tag` options:
245245

246246
```elixir
247-
{:github_api, "owner/repo", username: "skroob", token: "1234567", tag: "v0.1.0"}
247+
{:github_api, "owner/repo", token: "1234567", tag: "v0.1.0"}
248248
```
249249

250250
Artifact sites can pass options as a third parameter for adding headers

lib/nerves/artifact.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,10 +237,10 @@ defmodule Nerves.Artifact do
237237
Artifact sites can pass options as a third parameter for adding headers
238238
or query string parameters. For example, if you are trying to resolve
239239
artifacts hosted in a private Github repo, use `:github_api` and
240-
pass a user, tag, and personal access token into the sites helper:
240+
pass a tag and personal access token into the sites helper:
241241
242242
```elixir
243-
{:github_api, "owner/repo", username: "skroob", token: "1234567", tag: "v0.1.0"}
243+
{:github_api, "owner/repo", token: "1234567", tag: "v0.1.0"}
244244
```
245245
246246
Or pass query parameters for the URL:
@@ -419,7 +419,7 @@ defmodule Nerves.Artifact do
419419
420420
Supported artifact sites:
421421
{:github_releases, "owner/repo"}
422-
{:github_api, "owner/repo", username: "skroob", token: "1234567", tag: "v0.1.0"}
422+
{:github_api, "owner/repo", token: "1234567", tag: "v0.1.0"}
423423
{:gitea_releases, "host/owner/repo"},
424424
{:gitea_api, "owner/repo", base_url: "https://gitea.com", token: "123456", tag: "v0.1.0"}
425425
{:prefix, "http://myserver.com/artifacts"}

lib/nerves/artifact/resolvers/github_api.ex

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ defmodule Nerves.Artifact.Resolvers.GithubAPI do
2222
repo: nil,
2323
tag: "",
2424
token: "",
25-
url: nil,
26-
username: ""
25+
url: nil
2726

2827
@impl Nerves.Artifact.Resolver
2928
def get({org_proj, opts}, dest_path) do
@@ -40,13 +39,8 @@ defmodule Nerves.Artifact.Resolvers.GithubAPI do
4039
if opts.public? do
4140
[]
4241
else
43-
# make safe values here in case nil was supplied as an option
44-
# The request will fail and error will be reported later on
45-
user = opts.username || ""
4642
token = opts.token || ""
47-
48-
credentials = Base.encode64(user <> ":" <> token)
49-
[{"Authorization", "Basic " <> credentials}]
43+
[{"Authorization", "Bearer " <> token}]
5044
end
5145

5246
%{

test/nerves/artifact/resolvers_github_api_test.exs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,7 @@ defmodule Nerves.Artifact.Resolvers.GithubAPITest do
138138

139139
HTTPClient
140140
|> expect(:get, fn _url, opts ->
141-
[{"Authorization", "Basic " <> encoded}] = opts[:headers]
142-
[_, req_token] = String.split(Base.decode64!(encoded), ":")
141+
[{"Authorization", "Bearer " <> req_token}] = opts[:headers]
143142
assert req_token == env_token
144143
{:ok, @no_artifacts_response}
145144
end)
@@ -162,12 +161,7 @@ defmodule Nerves.Artifact.Resolvers.GithubAPITest do
162161

163162
HTTPClient
164163
|> expect(:get, fn _url, opts ->
165-
# A bit hacky since you need to know the internals, but this
166-
# breaks apart the Authorization header that was created with
167-
# the token given to the request and confirms it is the one
168-
# we wanted
169-
[{"Authorization", "Basic " <> encoded}] = opts[:headers]
170-
[_, req_token] = String.split(Base.decode64!(encoded), ":")
164+
[{"Authorization", "Bearer " <> req_token}] = opts[:headers]
171165
assert req_token == env_token
172166
{:ok, ""}
173167
end)

test/nerves/artifact_test.exs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,49 @@ defmodule Nerves.ArtifactTest do
107107
assert String.ends_with?(opts[:artifact_name], checksum_short <> Artifact.ext(pkg))
108108
end
109109

110+
test "GitHub API artifact sites are expanded" do
111+
repo = "nerves-project/system"
112+
113+
pkg = %{
114+
app: "my_system",
115+
version: "1.0.0",
116+
path: "./",
117+
config: [
118+
artifact_sites: [{:github_api, repo, token: "ghp_fake123", tag: "v1.0.0"}]
119+
]
120+
}
121+
122+
checksum_short = Nerves.Artifact.checksum(pkg, short: 7)
123+
124+
[{GithubAPI, {^repo, opts}}] = Artifact.expand_sites(pkg)
125+
assert opts[:token] == "ghp_fake123"
126+
assert opts[:tag] == "v1.0.0"
127+
assert String.ends_with?(opts[:artifact_name], checksum_short <> Artifact.ext(pkg))
128+
end
129+
130+
test "GitHub API artifact sites are expanded and username is ignored" do
131+
repo = "nerves-project/system"
132+
133+
pkg = %{
134+
app: "my_system",
135+
version: "1.0.0",
136+
path: "./",
137+
config: [
138+
artifact_sites: [
139+
{:github_api, repo,
140+
username: "removed_when_switch_to_bearer_auth", token: "ghp_fake123", tag: "v1.0.0"}
141+
]
142+
]
143+
}
144+
145+
checksum_short = Nerves.Artifact.checksum(pkg, short: 7)
146+
147+
[{GithubAPI, {^repo, opts}}] = Artifact.expand_sites(pkg)
148+
assert opts[:token] == "ghp_fake123"
149+
assert opts[:tag] == "v1.0.0"
150+
assert String.ends_with?(opts[:artifact_name], checksum_short <> Artifact.ext(pkg))
151+
end
152+
110153
test "Gitea artifact sites are expanded" do
111154
repo = "gitea.com/jmshrtn/nerves_artifact_test"
112155

0 commit comments

Comments
 (0)