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

.NET SDK: Uniservice, New Login, Webhooks (#615, #666, #719) #744

Merged
merged 3 commits into from
Jun 13, 2022
Merged
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
79 changes: 77 additions & 2 deletions dotnet/Trinsic/AccountService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ internal AccountService(ITokenProvider tokenProvider, IOptions<ServiceOptions> o
private Account.AccountClient Client { get; }

/// <summary>
/// Deprecated. Use LoginAsync instead.
///
/// Perform a sign-in to obtain an account profile. If the <see cref="AccountDetails" /> are
/// specified, they will be used to associate
/// </summary>
Expand All @@ -54,6 +56,8 @@ internal AccountService(ITokenProvider tokenProvider, IOptions<ServiceOptions> o
}

/// <summary>
/// Deprecated. Use Login instead.
///
/// Perform a sign-in to obtain an account profile. If the <see cref="AccountDetails" /> are
/// specified, they will be used to associate
/// </summary>
Expand All @@ -79,7 +83,7 @@ internal AccountService(ITokenProvider tokenProvider, IOptions<ServiceOptions> o
public static string Unprotect(string authToken, string securityCode) {
var profile = AccountProfile.Parser.ParseFrom(Base64Url.DecodeBytes(authToken));

UnBlindOberonTokenRequest request = new() {Token = profile.AuthToken};
UnBlindOberonTokenRequest request = new() { Token = profile.AuthToken };
request.Blinding.Add(ByteString.CopyFromUtf8(securityCode));
var result = Oberon.UnblindToken(request);

Expand All @@ -101,7 +105,7 @@ internal AccountService(ITokenProvider tokenProvider, IOptions<ServiceOptions> o
public static string Protect(string authToken, string securityCode) {
var profile = AccountProfile.Parser.ParseFrom(Base64Url.DecodeBytes(authToken));

BlindOberonTokenRequest request = new() {Token = profile.AuthToken};
BlindOberonTokenRequest request = new() { Token = profile.AuthToken };
request.Blinding.Add(ByteString.CopyFromUtf8(securityCode));
var result = Oberon.BlindToken(request);

Expand All @@ -114,6 +118,57 @@ internal AccountService(ITokenProvider tokenProvider, IOptions<ServiceOptions> o
return Base64Url.Encode(profile.ToByteArray());
}

/// <summary>
/// Logs in to the specified account; a new account will be created if it does not exist.
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public async Task<LoginResponse> LoginAsync(LoginRequest request) {
if (string.IsNullOrWhiteSpace(request.EcosystemId))
request.EcosystemId = Options.DefaultEcosystem;

var response = await Client.LoginAsync(request);

if (response.ResponseCase == LoginResponse.ResponseOneofCase.Profile)
{
await TokenProvider.SaveAsync(Base64Url.Encode(response.Profile.ToByteArray()));
}

return response;
}

/// <summary>
/// Logs in to the specified account; a new account will be created if it does not exist.
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public LoginResponse Login(LoginRequest request) {
if (string.IsNullOrWhiteSpace(request.EcosystemId))
request.EcosystemId = Options.DefaultEcosystem;

var response = Client.Login(request);

return response;
}

/// <summary>
/// Finalizes login from a previous `LoginRequest`.
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public async Task<LoginConfirmResponse> LoginConfirmAsync(LoginConfirmRequest request) {
return await Client.LoginConfirmAsync(request);
}

/// <summary>
/// Finalizes login from a previous `LoginRequest`.
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public LoginConfirmResponse LoginConfirm(LoginConfirmRequest request) {
return Client.LoginConfirm(request);
}

/// <summary>
/// Return the details about the currently active account
/// </summary>
Expand All @@ -136,6 +191,26 @@ internal AccountService(ITokenProvider tokenProvider, IOptions<ServiceOptions> o
return response;
}

/// <summary>
/// Authorizes provider of account's ecosystem to receive webhooks for
/// specified events regarding account.
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public async Task<AuthorizeWebhookResponse> AuthorizeWebhookAsync(AuthorizeWebhookRequest request) {
return await Client.AuthorizeWebhookAsync(request, await BuildMetadataAsync(request));
}

/// <summary>
/// Authorizes provider of account's ecosystem to receive webhooks for
/// specified events regarding account.
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public AuthorizeWebhookResponse AuthorizeWebhook(AuthorizeWebhookRequest request) {
return Client.AuthorizeWebhook(request, BuildMetadata(request));
}

public async Task<ListDevicesResponse> ListDevicesAsync() {
ListDevicesRequest request = new();
return await Client.ListDevicesAsync(request, await BuildMetadataAsync(request));
Expand Down
121 changes: 118 additions & 3 deletions dotnet/Trinsic/ProviderService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Threading.Tasks;
using Google.Protobuf;
using Microsoft.Extensions.Options;
Expand Down Expand Up @@ -81,7 +81,8 @@ internal ProviderService(ITokenProvider tokenProvider, IOptions<ServiceOptions>

var authToken = Base64Url.Encode(response.Profile.ToByteArray());

if (!response.Profile.Protection?.Enabled ?? true) {
if (!response.Profile.Protection?.Enabled ?? true)
{
Options.AuthToken = authToken;
await TokenProvider.SaveAsync(authToken);
}
Expand All @@ -99,13 +100,127 @@ internal ProviderService(ITokenProvider tokenProvider, IOptions<ServiceOptions>
var response = Client.CreateEcosystem(request);
var authToken = Base64Url.Encode(response.Profile.ToByteArray());

if (!response.Profile.Protection?.Enabled ?? true) {
if (!response.Profile.Protection?.Enabled ?? true)
{
Options.AuthToken = authToken;
TokenProvider.Save(authToken);
}
return (response.Ecosystem, authToken);
}

/// <summary>
/// Updates ecosystem ID and/or Description
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public async Task<UpdateEcosystemResponse> UpdateEcosystemAsync(UpdateEcosystemRequest request) {
return await Client.UpdateEcosystemAsync(request, await BuildMetadataAsync(request));
}

/// <summary>
/// Updates ecosystem ID and/or Description
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public UpdateEcosystemResponse UpdateEcosystem(UpdateEcosystemRequest request) {
return Client.UpdateEcosystem(request, BuildMetadata(request));
}

/// <summary>
/// Fetches information about the given ecosystem
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public async Task<EcosystemInfoResponse> EcosystemInfoAsync(EcosystemInfoRequest? request = null) {
request ??= new() { EcosystemId = Options.DefaultEcosystem };

return await Client.EcosystemInfoAsync(request, await BuildMetadataAsync(request));
}

/// <summary>
/// Fetches information about the given ecosystem
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public EcosystemInfoResponse EcosystemInfo(EcosystemInfoRequest? request = null) {
request ??= new() { EcosystemId = Options.DefaultEcosystem };

return Client.EcosystemInfo(request, BuildMetadata(request));
}

/// <summary>
/// Adds a webhook to the ecosystem
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public async Task<AddWebhookResponse> AddWebhookAsync(AddWebhookRequest request) {
return await Client.AddWebhookAsync(request, await BuildMetadataAsync(request));
}

/// <summary>
/// Adds a webhook to the ecosystem
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public AddWebhookResponse AddWebhook(AddWebhookRequest request) {
return Client.AddWebhook(request, BuildMetadata(request));
}

/// <summary>
/// Deletes a webhook from the ecosystem
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public async Task<DeleteWebhookResponse> DeleteWebhookAsync(DeleteWebhookRequest request) {
return await Client.DeleteWebhookAsync(request, await BuildMetadataAsync(request));
}

/// <summary>
/// Deletes a webhook from the ecosystem
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public DeleteWebhookResponse DeleteWebhook(DeleteWebhookRequest request) {
return Client.DeleteWebhook(request, BuildMetadata(request));
}

/// <summary>
/// Gets an event token for event streaming
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public async Task<GetEventTokenResponse> GetEventTokenAsync(GetEventTokenRequest request) {
return await Client.GetEventTokenAsync(request, await BuildMetadataAsync(request));
}

/// <summary>
/// Gets an event token for event streaming
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public GetEventTokenResponse GetEventToken(GetEventTokenRequest request) {
return Client.GetEventToken(request, BuildMetadata(request));
}

/// <summary>
/// Fetches the public key used to verify Oberon authentication tokens
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public async Task<GetOberonKeyResponse> GetOberonKeyAsync(GetOberonKeyRequest request) {
return await Client.GetOberonKeyAsync(request, await BuildMetadataAsync(request));
}

/// <summary>
/// Fetches the public key used to verify Oberon authentication tokens
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public GetOberonKeyResponse GetOberonKey(GetOberonKeyRequest request) {
return Client.GetOberonKey(request, BuildMetadata(request));
}


/// <summary>
/// Generates an unprotected authentication token that can be used
/// to configure server side applications
Expand Down
5 changes: 4 additions & 1 deletion dotnet/Trinsic/ServiceBase.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Diagnostics.CodeAnalysis;
using Grpc.Core;
using Google.Protobuf;
Expand Down Expand Up @@ -60,6 +60,9 @@ public abstract class ServiceBase

private readonly ISecurityProvider _securityProvider = new OberonSecurityProvider();

/// <summary>
/// Gets the options set on this service.
/// </summary>
public ServiceOptions Options { get; }

/// <summary>
Expand Down
Loading