Skip to content

Commit d24bd67

Browse files
authored
Give a better error message if token contains newlines (#221)
I constructed a token as `OAuth2(read("token", String))`, but my token file had a trailing newline. GitHub stops parsing headers after an empty line, so depending on where the header ended up in the list of headers, this was causing all sorts of strange errors. Detect this situation and throw an intelligent error in GitHub.jl instead.
1 parent c586ca7 commit d24bd67

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

src/utils/auth.jl

+14
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ abstract type Authorization end
77
# TODO: SecureString on 0.7
88
struct OAuth2 <: Authorization
99
token::String
10+
function OAuth2(token)
11+
token = convert(String, token)
12+
if !all(c->isascii(c) && !isspace(c), token)
13+
throw(ArgumentError("token `$token` has invalid whitespace or non-ascii character."))
14+
end
15+
new(token)
16+
end
1017
end
1118

1219
struct UsernamePassAuth <: Authorization
@@ -18,6 +25,13 @@ struct AnonymousAuth <: Authorization end
1825

1926
struct JWTAuth <: Authorization
2027
JWT::String
28+
function JWTAuth(token)
29+
token = convert(String, token)
30+
if !all(c->isascii(c) && !isspace(c), token)
31+
throw(ArgumentError("ArgumentError token `$token` has invalid whitespace or non-ascii character."))
32+
end
33+
new(token)
34+
end
2135
end
2236

2337
####################

test/auth_tests.jl

+3
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,6 @@ auth2 = GitHub.JWTAuth(1234, key; iat = DateTime("2016-9-15T14:00"))
2121
# test to make sure things don't break.
2222
@test auth.JWT == correct_jwt
2323
@test auth2.JWT == correct_jwt
24+
25+
@test_throws ArgumentError GitHub.OAuth2("ghp_\n")
26+
@test_throws ArgumentError GitHub.JWTAuth("ghp_\n")

0 commit comments

Comments
 (0)