-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(generic): check expiry in JWT password values
add minimal JWT data class for content decoding and extraction add decode support to Base64Url converter override GenericHostProvider credential query to check for JWT content add expiry check for JWT refresh token
- Loading branch information
Showing
3 changed files
with
102 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
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) | ||
{ | ||
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; | ||
|
||
static public WebToken TryCreate(string value) | ||
{ | ||
try | ||
{ | ||
var parts = value.Split('.'); | ||
if (parts.Length != 3) | ||
{ | ||
return null; | ||
} | ||
var header = JsonSerializer.Deserialize<TokenHeader>(Base64UrlConvert.Decode(parts[0])); | ||
if (!"JWT".Equals(header.Type)) | ||
{ | ||
return null; | ||
} | ||
var payload = JsonSerializer.Deserialize<TokenPayload>(Base64UrlConvert.Decode(parts[1])); | ||
return new WebToken(header, payload, parts[2]); | ||
} | ||
catch | ||
{ | ||
return null; | ||
} | ||
} | ||
|
||
static public bool IsExpiredToken(string value) | ||
{ | ||
var token = TryCreate(value); | ||
return token != null && token.Payload.Expiry < DateTimeOffset.Now.ToUnixTimeSeconds(); | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters