Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Give a better error message if token contains newlines #221

Merged
merged 1 commit into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/utils/auth.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ abstract type Authorization end
# TODO: SecureString on 0.7
struct OAuth2 <: Authorization
token::String
function OAuth2(token)
token = convert(String, token)
if !all(c->isascii(c) && !isspace(c), token)
throw(ArgumentError("token `$token` has invalid whitespace or non-ascii character."))
end
new(token)
end
end

struct UsernamePassAuth <: Authorization
Expand All @@ -18,6 +25,13 @@ struct AnonymousAuth <: Authorization end

struct JWTAuth <: Authorization
JWT::String
function JWTAuth(token)
token = convert(String, token)
if !all(c->isascii(c) && !isspace(c), token)
throw(ArgumentError("ArgumentError token `$token` has invalid whitespace or non-ascii character."))
end
new(token)
end
end

####################
Expand Down
3 changes: 3 additions & 0 deletions test/auth_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@ auth2 = GitHub.JWTAuth(1234, key; iat = DateTime("2016-9-15T14:00"))
# test to make sure things don't break.
@test auth.JWT == correct_jwt
@test auth2.JWT == correct_jwt

@test_throws ArgumentError GitHub.OAuth2("ghp_\n")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In theory would be good to test the JWTAuth constructor as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure

@test_throws ArgumentError GitHub.JWTAuth("ghp_\n")
Loading