Skip to content

Commit

Permalink
Support JWT sub and acl
Browse files Browse the repository at this point in the history
  • Loading branch information
smithrobs committed Jul 21, 2018
1 parent 02965c9 commit 80ed233
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
27 changes: 25 additions & 2 deletions Nexmo.Api/Jwt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Nexmo.Api
{
internal class Jwt
{
internal static string CreateToken(string appId, string privateKey)
internal static string CreateToken(string appId, string privateKey, string sub = null)
{
var tokenData = new byte[64];
var rng = RandomNumberGenerator.Create();
Expand All @@ -18,9 +18,32 @@ internal static string CreateToken(string appId, string privateKey)
{
{ "iat", (long) (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds },
{ "application_id", appId },
{ "jti", jwtTokenId }
{ "jti", jwtTokenId },
// TODO: Hardcoded
{ "acl", new Dictionary<string, object>
{
{ "paths", new Dictionary<string, object>
{
{ "/v1/users/**", new {} },
{ "/v1/conversations/**", new {} },
{ "/v1/sessions/**", new {} },
{ "/v1/devices/**", new {} },
{ "/v1/image/**", new {} },
{ "/v3/media/**", new {} },
{ "/v1/applications/**", new {} },
{ "/v1/push/**", new {} },
{ "/v1/knocking/**", new {} },
}
}
}
}
};

if (!string.IsNullOrEmpty(sub))
{
payload["sub"] = sub;
}

var rsa = PemParse.DecodePEMKey(privateKey);

return JWT.Encode(payload, rsa, JwsAlgorithm.RS256);
Expand Down
5 changes: 5 additions & 0 deletions Nexmo.Api/Request/Credentials.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ public class Credentials
/// </summary>
public string AppUserAgent { get; set; }

/// <summary>
/// (Optional) sub (Subject) to use when generating a JWT
/// </summary>
public string JwtSubject { get; set; }

public Credentials (string nexmoApiKey, string nexmoApiSecret)
{
ApiKey = nexmoApiKey;
Expand Down
4 changes: 2 additions & 2 deletions Nexmo.Api/Request/VersionedApiRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ private static string DoRequest(Uri uri, Credentials creds = null)
SetUserAgent(ref req, creds);
// attempt bearer token auth
req.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer",
Jwt.CreateToken(appId, appKeyPath));
Jwt.CreateToken(appId, appKeyPath, creds?.JwtSubject ?? string.Empty));

using (LogProvider.OpenMappedContext("VersionedApiRequest.DoRequest",uri.GetHashCode()))
{
Expand Down Expand Up @@ -133,7 +133,7 @@ public static NexmoResponse DoRequest(string method, Uri uri, object payload, Cr
SetUserAgent(ref req, creds);
// attempt bearer token auth
req.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer",
Jwt.CreateToken(appId, appKeyPath));
Jwt.CreateToken(appId, appKeyPath, creds?.JwtSubject ?? string.Empty));

var data = Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(payload,
Formatting.None, new JsonSerializerSettings { DefaultValueHandling = DefaultValueHandling.Ignore }));
Expand Down

0 comments on commit 80ed233

Please sign in to comment.