Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: adding function to check if the jwt token is expired or not #753

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ var message = MessageResource.Create(
Console.WriteLine(message.Sid);
```

Examples on how to make rest calls with bearer token authentication is added [here][examples/BearerTokenAuthentication.md]

## Specify Region and/or Edge

To take advantage of Twilio's [Global Infrastructure](https://www.twilio.com/docs/global-infrastructure), specify the target Region and/or Edge for the client:
Expand All @@ -82,7 +84,7 @@ TwilioClient.SetRegion("au1");
TwilioClient.SetEdge("sydney");
```

This will result in the `hostname` transforming from `api.twilio.com` to `api.sydney.au1.twilio.com`.
This will result in the `hostname` transforming from `api.twilio.com` to `api.sydney.au1.twilio.com`. Use appropriate client depending on the type of authentication used

## Enable debug logging

Expand Down
21 changes: 21 additions & 0 deletions examples/BearerTokenAuthentication.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using Twilio;
using Twilio.Base;
using Twilio.Exceptions;
using Twilio.Rest.Api.V2010.Account;
using Twilio.Rest.PreviewIam.Organizations;

//Find client id, client secret and organisation sid from admin center of your organisation
//path account sid is the sid of the account withing the organisation
class Program
{
static void Main(string[] args)
{
TwilioOrgsTokenAuthClient.Init(GRANT_TYPE, CLIENT_ID, CLIENT_SECRET);
Twilio.Base.BearerToken.BearerTokenResourceSet<Twilio.Rest.PreviewIam.Organizations.AccountResource> accountList = null;
accountList = Twilio.Rest.PreviewIam.Organizations.AccountResource.Read(pathOrganizationSid: ORGS_SID);
Console.WriteLine(accountList.ElementAt(0).FriendlyName);
var account = Twilio.Rest.PreviewIam.Organizations.AccountResource.Fetch(pathOrganizationSid: ORGS_SID, pathAccountSid: PATH_ACCOUNT_SID);
Console.WriteLine(account.FriendlyName);
}
}
122 changes: 122 additions & 0 deletions src/Twilio/Base/BearerToken/BearerTokenResourceSet.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
using System;
using System.Reflection;
using System.Collections.Generic;
using Twilio.Clients;
using Twilio.Clients.BearerToken;

namespace Twilio.Base.BearerToken
{
/// <summary>
/// A collection of resources of type T
/// </summary>
///
/// <typeparam name="T">Resource Type</typeparam>
public class BearerTokenResourceSet<T> : IEnumerable<T> where T : Resource
{
/// <summary>
/// Automatically iterate through pages of results
/// </summary>
public bool AutoPaging { get; set; }

private readonly TwilioBearerTokenRestClient _client;
private readonly ReadOptions<T> _options;
private readonly long _pageLimit;

private long _pages;
private long _processed;
private Page<T> _page;
private IEnumerator<T> _iterator;

/// <summary>
/// Create a new resource set
/// </summary>
///
/// <param name="page">Page of resources</param>
/// <param name="options">Read options</param>
/// <param name="client">Client to make requests</param>
public BearerTokenResourceSet(Page<T> page, ReadOptions<T> options, TwilioBearerTokenRestClient client)
{
_page = page;
_options = options;
_client = client;

_iterator = page.Records.GetEnumerator();
_processed = 0;
_pages = 1;
_pageLimit = long.MaxValue;

AutoPaging = true;

if (_options.Limit != null)
{
_pageLimit = (long) (Math.Ceiling((double) _options.Limit.Value / page.PageSize));
}
}

/// <summary>
/// Get iterator for resources
/// </summary>
///
/// <returns>IEnumerator of resources</returns>
public IEnumerator<T> GetEnumerator()
{
while (_page != null)
{
_iterator.Reset();
while (_iterator.MoveNext())
{
// Exit if we've reached item limit
if (_options.Limit != null && _processed > _options.Limit.Value)
{
yield break;
}

_processed++;
yield return _iterator.Current;
}

if (AutoPaging && _page.HasNextPage())
{
FetchNextPage();
}
else
{
break;
}
}
}

/// <summary>
/// Get iterator for resources
/// </summary>
///
/// <returns>IEnumerator of resources</returns>
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return GetEnumerator();
}

private void FetchNextPage()
{
if (!_page.HasNextPage() || _pages >= _pageLimit)
{
_page = null;
_iterator = null;
return;
}

_pages++;
_page = (Page<T>)GetNextPage().Invoke(null, new object[]{ _page, _client });
_iterator = _page.Records.GetEnumerator();
}

private static MethodInfo GetNextPage()
{
#if !NET35
return typeof(T).GetRuntimeMethod("NextPage", new[]{ typeof(Page<T>), typeof(TwilioBearerTokenRestClient) });
#else
return typeof(T).GetMethod("NextPage", new[]{ typeof(Page<T>), typeof(TwilioBearerTokenRestClient) });
#endif
}
}
}
30 changes: 30 additions & 0 deletions src/Twilio/Clients/Base64UrlEncoder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#if NET35
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.Script.Serialization;

namespace Twilio.Clients{

public abstract class Base64UrlEncoder
{
public static string Decode(string base64Url)
{
// Replace URL-safe characters with Base64 characters
string base64 = base64Url
.Replace('-', '+')
.Replace('_', '/');

// Add padding if necessary
switch (base64.Length % 4)
{
case 2: base64 += "=="; break;
case 3: base64 += "="; break;
}

byte[] bytes = Convert.FromBase64String(base64);
return Encoding.UTF8.GetString(bytes);
}
}
}
#endif
Loading
Loading