Skip to content

Commit

Permalink
Merge pull request #19 from Bynder/permanent_tokens
Browse files Browse the repository at this point in the history
Added permanent tokens logic
  • Loading branch information
Daniel Sequeira authored Nov 13, 2019
2 parents 5ffcad6 + 71d2fd9 commit 31f0254
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 23 deletions.
23 changes: 20 additions & 3 deletions Bynder/Sample/ApiSample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,35 @@ public class ApiSample
/// <param name="args">arguments to main</param>
public static void Main(string[] args)
{
using(var waitForToken = new WaitForToken())
using (var client = ClientFactory.Create(Configuration.FromJson("Config.json")))
{
var mediaList = client.GetAssetService().GetMediaListAsync(new MediaQuery()).Result;

foreach (var media in mediaList)
{
Console.WriteLine(media.Name);
}

var collectionList = client.GetCollectionService().GetCollectionsAsync(new GetCollectionsQuery()).Result;

foreach (var collection in collectionList)
{
Console.WriteLine(collection.Name);
}
}

using (var waitForToken = new WaitForToken())
{
using (var listener = new UrlHttpListener("http://localhost:8888/", waitForToken))
{
using (var client = ClientFactory.Create(Configuration.FromJson("Config.json")))
{
Browser.Launch(client.GetOAuthService().GetAuthorisationUrl("state example", "openid offline"));
Browser.Launch(client.GetOAuthService().GetAuthorisationUrl("state example", "offline asset:read collection:read"));
waitForToken.WaitHandle.WaitOne(50000);

if (waitForToken.Success)
{
client.GetOAuthService().GetAccessTokenAsync(waitForToken.Token, "openid offline").Wait();
client.GetOAuthService().GetAccessTokenAsync(waitForToken.Token, "offline asset:read collection:read").Wait();

var mediaList = client.GetAssetService().GetMediaListAsync(new MediaQuery()).Result;

Expand Down
5 changes: 3 additions & 2 deletions Bynder/Sample/Config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"base_url": "https://example.bynder.com",
"client_id": "your oauth app client id",
"client_secret": "your oauth app client secret",
"redirect_uri": "your oauth app redirect uri"
}
"redirect_uri": "your oauth app redirect uri",
"permanent_token": "your permanent token"
}
2 changes: 1 addition & 1 deletion Bynder/Sdk/Api/RequestSender/ApiRequestSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ private async Task RefreshToken()
{
Authenticated = false,
Query = query,
Path = $"/oauth2/token",
Path = $"/v6/authentication/oauth2/token",
HTTPMethod = HttpMethod.Post
};

Expand Down
8 changes: 0 additions & 8 deletions Bynder/Sdk/Model/Token.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,6 @@ public class Token
[JsonProperty("expires_in")]
public int ExpiresIn { get; set; }

/// <summary>
/// Gets or sets the id of the token.
/// </summary>
/// <value>The id of the token.</value>
[JsonProperty("id_token")]
public string TokenId { get; set; }

/// <summary>
/// Gets or sets the type of the token.
/// </summary>
Expand Down Expand Up @@ -68,7 +61,6 @@ public DateTimeOffset GetAccessTokenExpiration()
/// <summary>
/// Sets the access token expiration according to its ExpiresIn value.
/// </summary>
/// <param name="value">The access token expiration.</param>
public void SetAccessTokenExpiration()
{
AccessTokenExpiration = DateTimeOffset.Now.AddSeconds(ExpiresIn);
Expand Down
9 changes: 8 additions & 1 deletion Bynder/Sdk/Service/BynderClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,14 @@ public BynderClient(Configuration configuration)
{
new ConfigurationValidator().Validate(configuration);
_configuration = configuration;
_credentials = new Credentials(configuration.Token);
if (configuration.PermanentToken != null)
{
_credentials = new Credentials(configuration.PermanentToken);
}
else
{
_credentials = new Credentials(configuration.Token);
}
_requestSender = ApiRequestSender.Create(_configuration, _credentials);
}

Expand Down
6 changes: 6 additions & 0 deletions Bynder/Sdk/Settings/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ public class Configuration
/// </summary>
public Token Token { get; set; }

/// <summary>
/// Permanent token
/// </summary>
[JsonProperty("permanent_token")]
public string PermanentToken { get; set; }

/// <summary>
/// Create a <see cref="Configuration"/> using the given filepath
/// </summary>
Expand Down
35 changes: 30 additions & 5 deletions Bynder/Sdk/Settings/Credentials.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,23 @@ internal class Credentials : ICredentials
private Token _token;

/// <summary>
/// Initializes a new instance of the <see cref="T:Credentials"/> class.
/// Initializes a new instance of the <see cref="T:Credentials"/> class with a OAuth token.
/// </summary>
/// <param name="token">Token passed in the configuration.</param>
/// <param name="token">OAuth token passed in the configuration.</param>
public Credentials(Token token)
{
_token = token;
}

/// <summary>
/// Initializes a new instance of the <see cref="T:Credentials"/> class with a permanent token.
/// </summary>
/// <param name="permanentToken">Permanent token passed in the configuration.</param>
public Credentials(string permanentToken)
{
PermanentToken = permanentToken;
}

/// <summary>
/// Check <see cref="t:ICredentials"/>.
/// </summary>
Expand All @@ -32,7 +41,7 @@ public Credentials(Token token)
/// Check <see cref="t:ICredentials"/>.
/// </summary>
/// <value>Check <see cref="t:ICredentials"/>.</value>
public string AccessToken => _token?.AccessToken;
public string AccessToken => PermanentToken ?? _token?.AccessToken;

/// <summary>
/// Check <see cref="t:ICredentials"/>.
Expand All @@ -56,7 +65,7 @@ public bool CanRefresh
/// Check <see cref="t:ICredentials"/>.
/// </summary>
/// <value>Check <see cref="t:ICredentials"/>.</value>
public string TokenType => _token?.TokenType;
public string TokenType => _token?.TokenType ?? "Bearer";

/// <summary>
/// Gets or sets the token that will be used to authenticate API calls.
Expand All @@ -79,12 +88,19 @@ private Token Token
}
}

private string PermanentToken { get; set; }

/// <summary>
/// Check <see cref="t:ICredentials"/>.
/// </summary>
/// <returns>Check <see cref="t:ICredentials"/>.</returns>
public bool AreValid()
{
if (PermanentToken != null)
{
return true;
}

if (_token != null
&& _token.AccessToken != null)
{
Expand All @@ -101,7 +117,16 @@ public bool AreValid()
/// <param name="token">Check <see cref="t:ICredentials"/>.</param>
public void Update(Token token)
{
Token = token;
if (Token == null)
{
Token = token;
}
else
{
token.RefreshToken = Token.RefreshToken;
Token = token;
}

}
}
}
6 changes: 3 additions & 3 deletions Bynder/Test/Service/OAuth/OAuthServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ public void GetAuthorisationUrlReturnsCorrectUrl()
RedirectUri = "https://redirect.bynder.com"
}, null, null);

var authorisationUrl = oauth.GetAuthorisationUrl("state example", "openid offline");
var authorisationUrl = oauth.GetAuthorisationUrl("state example", "offline");

Assert.Equal("https://example.bynder.com:443/v6/authentication/oauth2/auth?client_id=clientId&redirect_uri=https%3A%2F%2Fredirect.bynder.com&response_type=code&scope=openid%20offline&state=state%20example",
Assert.Equal("https://example.bynder.com:443/v6/authentication/oauth2/auth?client_id=clientId&redirect_uri=https%3A%2F%2Fredirect.bynder.com&response_type=code&scope=offline&state=state%20example",
authorisationUrl);
}

Expand All @@ -52,7 +52,7 @@ public async Task GetAccessTokenCallsRequestAsyncAndUpdates()
credentials.Object,
apiRequestSender.Object);

await oauthService.GetAccessTokenAsync("code", "openid offline").ConfigureAwait(false);
await oauthService.GetAccessTokenAsync("code", "offline").ConfigureAwait(false);

apiRequestSender.Verify(sender => sender.SendRequestAsync(
It.Is<OAuthRequest<Token>>(
Expand Down

0 comments on commit 31f0254

Please sign in to comment.