-
Notifications
You must be signed in to change notification settings - Fork 2.5k
fix(generic): check local token expiry #1837
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,60 @@ | ||||||
| using System; | ||||||
| using System.Text.Json; | ||||||
| using System.Text.Json.Serialization; | ||||||
|
|
||||||
| namespace GitCredentialManager.Authentication.Oauth.Json | ||||||
| { | ||||||
| public class WebToken(WebToken.TokenHeader header, WebToken.TokenPayload payload, string signature) | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of writing our own, could we not instead use the JsonWebToken type from the Microsoft.IdentityModel.JsonWebTokens package? We'd still need the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seemed easier to just add some simple code instead of depending on a full-blown JWT implementation. |
||||||
| { | ||||||
| public class TokenHeader | ||||||
| { | ||||||
| [JsonRequired] | ||||||
| [JsonInclude] | ||||||
| [JsonPropertyName("typ")] | ||||||
| public string Type { get; private set; } | ||||||
| } | ||||||
| public class TokenPayload | ||||||
| { | ||||||
| [JsonRequired] | ||||||
| [JsonInclude] | ||||||
| [JsonPropertyName("exp")] | ||||||
| public long Expiry { get; private set; } | ||||||
| } | ||||||
| public TokenHeader Header { get; } = header; | ||||||
| public TokenPayload Payload { get; } = payload; | ||||||
| public string Signature { get; } = signature; | ||||||
|
|
||||||
| public bool IsExpired | ||||||
| { | ||||||
| get | ||||||
| { | ||||||
| return Payload.Expiry < DateTimeOffset.Now.ToUnixTimeSeconds(); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| static public bool TryCreate(string value, out WebToken jwt) | ||||||
| { | ||||||
| jwt = null; | ||||||
| try | ||||||
| { | ||||||
| var parts = value.Split('.'); | ||||||
| if (parts.Length != 3) | ||||||
| { | ||||||
| return false; | ||||||
| } | ||||||
| var header = JsonSerializer.Deserialize<TokenHeader>(Base64UrlConvert.Decode(parts[0])); | ||||||
| if (!"JWT".Equals(header.Type)) | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should do a case insensitive comparison here.
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Stuck with the recommended variant, no problem expanding acceptance. |
||||||
| { | ||||||
| return false; | ||||||
| } | ||||||
| var payload = JsonSerializer.Deserialize<TokenPayload>(Base64UrlConvert.Decode(parts[1])); | ||||||
| jwt = new WebToken(header, payload, parts[2]); | ||||||
| return true; | ||||||
| } | ||||||
| catch | ||||||
| { | ||||||
| return false; | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, silly oversight.
Considering #268 may expand use to other providers, this is (if kept) maybe better to be renamed to
GitCredentialManager.Authentication.JsonWebToken?Only saw there was an existing related issue way after adding the initial PR as a base of discussion.