-
Notifications
You must be signed in to change notification settings - Fork 305
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add region to access token (#546)
- Loading branch information
1 parent
5a45223
commit 69d82ef
Showing
3 changed files
with
105 additions
and
32 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,72 @@ | ||
#if !NET35 | ||
using System.IdentityModel.Tokens.Jwt; | ||
#else | ||
using System; | ||
using System.Collections.Generic; | ||
#endif | ||
|
||
using System; | ||
using System.Security.Cryptography; | ||
using System.Text; | ||
using Newtonsoft.Json; | ||
|
||
namespace Twilio.Tests.Jwt | ||
{ | ||
class DecodedJwt | ||
{ | ||
#if !NET35 | ||
private static byte[] Base64UrlDecode(string input) => Convert.FromBase64String(UrlDecode(input)); | ||
|
||
public JwtPayload Payload | ||
private static string UrlDecode(string input) | ||
{ | ||
get | ||
var output = input; | ||
output = output.Replace('-', '+'); // 62nd char of encoding | ||
output = output.Replace('_', '/'); // 63rd char of encoding | ||
|
||
// Pad with trailing '='s | ||
switch (output.Length % 4) | ||
{ | ||
return token.Payload; | ||
case 0: | ||
break; // No pad chars in this case | ||
case 2: | ||
output += "=="; | ||
break; // Two pad chars | ||
case 3: | ||
output += "="; | ||
break; // One pad char | ||
default: | ||
throw new Exception($"Illegal base-64 string: '{input}'."); | ||
} | ||
|
||
return output; | ||
} | ||
|
||
private readonly JwtSecurityToken token; | ||
private static IDictionary<string, object> DecodeJwtPart(string part) | ||
{ | ||
var bytes = Base64UrlDecode(part); | ||
var json = Encoding.UTF8.GetString(bytes); | ||
var data = JsonConvert.DeserializeObject<Dictionary<string, object>>(json); | ||
|
||
public DecodedJwt(string jwt, string secret) | ||
return data; | ||
} | ||
|
||
public IDictionary<string, object> Header | ||
{ | ||
token = new JwtSecurityToken(jwt); | ||
get | ||
{ | ||
return _header; | ||
} | ||
} | ||
|
||
#else | ||
public IDictionary<string, object> Payload | ||
{ | ||
get | ||
{ | ||
return JsonConvert.DeserializeObject<Dictionary<string, object>>(token); | ||
return _payload; | ||
} | ||
} | ||
|
||
private readonly string token; | ||
private readonly IDictionary<string, object> _header; | ||
private readonly IDictionary<string, object> _payload; | ||
|
||
public DecodedJwt(string jwt, string secret) | ||
{ | ||
token = JWT.JsonWebToken.Decode(jwt, secret); | ||
var parts = jwt.Split('.'); | ||
_header = DecodeJwtPart(parts[0]); | ||
_payload = DecodeJwtPart(parts[1]); | ||
} | ||
|
||
#endif | ||
|
||
} | ||
} |