-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Expand file tree
/
Copy pathBitbucketTokenEndpointResponseJson.cs
More file actions
76 lines (68 loc) · 3.02 KB
/
Copy pathBitbucketTokenEndpointResponseJson.cs
File metadata and controls
76 lines (68 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using System;
using System.Text.Json;
using GitCredentialManager.Authentication.OAuth.Json;
using System.Text.Json.Serialization;
namespace Atlassian.Bitbucket
{
[JsonSerializable(typeof(BitbucketTokenEndpointResponseJson))]
public partial class BitbucketOAuthJsonContext : JsonSerializerContext;
[JsonConverter(typeof(BitbucketCustomTokenEndpointResponseJsonConverter))]
public class BitbucketTokenEndpointResponseJson : TokenEndpointResponseJson
{
// To ensure the "scopes" property used by Bitbucket is deserialized successfully with System.Text.Json, we must
// use a custom converter. Otherwise, ordering will matter (i.e. if "scopes" is the final property, its value
// will be used, but if "scope" is the final property, its value will be used).
}
public class BitbucketCustomTokenEndpointResponseJsonConverter : JsonConverter<BitbucketTokenEndpointResponseJson>
{
public override BitbucketTokenEndpointResponseJson Read(
ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType != JsonTokenType.StartObject)
{
throw new JsonException();
}
var response = new BitbucketTokenEndpointResponseJson();
while (reader.Read())
{
if (reader.TokenType == JsonTokenType.EndObject)
{
return response;
}
if (reader.TokenType == JsonTokenType.PropertyName)
{
var propertyName = reader.GetString();
reader.Read();
if (propertyName != null)
{
switch (propertyName)
{
case "access_token":
response.AccessToken = reader.GetString();
break;
case "token_type":
response.TokenType = reader.GetString();
break;
case "expires_in":
if (reader.TryGetUInt32(out var expiration))
response.ExpiresIn = expiration;
else
response.ExpiresIn = null;
break;
case "refresh_token":
response.RefreshToken = reader.GetString();
break;
case "scopes":
response.Scope = reader.GetString();
break;
}
}
}
}
throw new JsonException();
}
public override void Write(
Utf8JsonWriter writer, BitbucketTokenEndpointResponseJson tokenEndpointResponseJson, JsonSerializerOptions options)
{ }
}
}