-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
97af50d
commit 456e194
Showing
8 changed files
with
339 additions
and
91 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
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
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
109 changes: 109 additions & 0 deletions
109
Fonlow.Auth.PayloadConverters/TokenResponseConverter.cs
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,109 @@ | ||
using Fonlow.Auth.Models; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Fonlow.Text.Json.Auth | ||
{ | ||
public sealed class TokenResponseConverter : JsonConverter<TokenResponseBase> | ||
{ | ||
public override bool HandleNull => true; | ||
|
||
public override void Write(Utf8JsonWriter writer, TokenResponseBase value, JsonSerializerOptions options) | ||
{ | ||
writer.WriteStartObject(); | ||
writer.WriteString("token_type", value.TokenType); | ||
switch (value.TokenType) | ||
{ | ||
case "bearer": | ||
case "Bearer": | ||
AccessTokenResponse accessTokenResponse = value as AccessTokenResponse; | ||
writer.WriteString("access_token", accessTokenResponse.AccessToken); | ||
writer.WriteNumber("expires_in", Convert.ToDecimal(accessTokenResponse.ExpiresIn)); | ||
if (!string.IsNullOrWhiteSpace(accessTokenResponse.RefreshToken)) | ||
{ | ||
writer.WriteString("refresh_token", accessTokenResponse.RefreshToken); | ||
} | ||
|
||
if (!string.IsNullOrWhiteSpace(accessTokenResponse.Scope)) | ||
{ | ||
writer.WriteString("scope", accessTokenResponse.Scope); | ||
} | ||
break; | ||
default: | ||
throw new NotSupportedException(); | ||
} | ||
|
||
writer.WriteEndObject(); | ||
} | ||
|
||
public override TokenResponseBase Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
if (reader.TokenType != JsonTokenType.StartObject) | ||
{ | ||
throw new JsonException(); | ||
} | ||
|
||
reader.Read(); | ||
if (reader.TokenType != JsonTokenType.PropertyName) | ||
{ | ||
throw new JsonException(); | ||
} | ||
|
||
string propertyName = reader.GetString(); | ||
if (propertyName != "token_type") | ||
{ | ||
throw new JsonException(); | ||
} | ||
|
||
reader.Read(); | ||
if (reader.TokenType != JsonTokenType.String) | ||
{ | ||
throw new JsonException(); | ||
} | ||
|
||
var typeDiscriminator = reader.GetString(); | ||
|
||
switch (typeDiscriminator) | ||
{ | ||
case "bearer": | ||
case "Bearer": | ||
var accessTokenResponse = new AccessTokenResponse(); | ||
accessTokenResponse.TokenType = typeDiscriminator; | ||
while (reader.Read()) | ||
{ | ||
if (reader.TokenType == JsonTokenType.EndObject) | ||
{ | ||
return accessTokenResponse; | ||
} | ||
|
||
if (reader.TokenType == JsonTokenType.PropertyName) | ||
{ | ||
propertyName = reader.GetString(); | ||
if (reader.Read()) | ||
{ | ||
switch (propertyName) | ||
{ | ||
case "access_token": | ||
accessTokenResponse.AccessToken = reader.GetString(); | ||
break; | ||
case "refresh_token": | ||
accessTokenResponse.AccessToken = reader.GetString(); | ||
break; | ||
case "expires_in": | ||
accessTokenResponse.ExpiresIn = reader.GetInt32(); | ||
break; | ||
case "scope": | ||
accessTokenResponse.Scope = reader.GetString(); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
return accessTokenResponse; | ||
default: | ||
throw new JsonException(); | ||
} | ||
} | ||
} | ||
} |
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
Oops, something went wrong.