Skip to content

Commit

Permalink
feat: add auth client (#476)
Browse files Browse the repository at this point in the history
* feat: adding auth client

* chore: adding auth config

* chore: building out response class for disposable token

* chore: add token client and fix names where appropriate

* chore: add logging utils for auth requests

* chore: more configuration work

* chore: AuthGrpcManager WIP

* chore: flesh out expiry classes

* chore: quick fixes

* chore: add token endpoint to auth provider

* chore: add validation for ExpiresIn

* chore: getting disposable token data back from backend

* chore: adding tests

* fix: add missing exception message

* fix: properly encode token

* chore: add overload for ExpiresIn.Epoch() that takes an int

* fix: clean up configurations names

* fix: misc cleanup

* fix: update config method for test

* fix: assign topic endpoint in string auth provider

* chore: comment cleanup
  • Loading branch information
pgautier404 authored Sep 8, 2023
1 parent 80d95f4 commit b2fc063
Show file tree
Hide file tree
Showing 29 changed files with 1,751 additions and 5 deletions.
20 changes: 20 additions & 0 deletions src/Momento.Sdk/Auth/AccessControl/DisposableToken.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace Momento.Sdk.Auth.AccessControl;

public abstract record DisposableTokenPermission;

Check warning on line 3 in src/Momento.Sdk/Auth/AccessControl/DisposableToken.cs

View workflow job for this annotation

GitHub Actions / build_csharp (ubuntu-latest, net6.0)

Missing XML comment for publicly visible type or member 'DisposableTokenPermission'

Check warning on line 3 in src/Momento.Sdk/Auth/AccessControl/DisposableToken.cs

View workflow job for this annotation

GitHub Actions / build_csharp (windows-latest, net461)

Missing XML comment for publicly visible type or member 'DisposableTokenPermission'

public abstract record DisposableToken

Check warning on line 5 in src/Momento.Sdk/Auth/AccessControl/DisposableToken.cs

View workflow job for this annotation

GitHub Actions / build_csharp (ubuntu-latest, net6.0)

Missing XML comment for publicly visible type or member 'DisposableToken'

Check warning on line 5 in src/Momento.Sdk/Auth/AccessControl/DisposableToken.cs

View workflow job for this annotation

GitHub Actions / build_csharp (windows-latest, net461)

Missing XML comment for publicly visible type or member 'DisposableToken'
{
public record CachePermission(CacheRole Role, CacheSelector CacheSelector) : DisposableTokenPermission

Check warning on line 7 in src/Momento.Sdk/Auth/AccessControl/DisposableToken.cs

View workflow job for this annotation

GitHub Actions / build_csharp (ubuntu-latest, net6.0)

Missing XML comment for publicly visible type or member 'DisposableToken.CachePermission'

Check warning on line 7 in src/Momento.Sdk/Auth/AccessControl/DisposableToken.cs

View workflow job for this annotation

GitHub Actions / build_csharp (ubuntu-latest, net6.0)

Missing XML comment for publicly visible type or member 'DisposableToken.CachePermission.CachePermission(CacheRole, CacheSelector)'

Check warning on line 7 in src/Momento.Sdk/Auth/AccessControl/DisposableToken.cs

View workflow job for this annotation

GitHub Actions / build_csharp (ubuntu-latest, net6.0)

Missing XML comment for publicly visible type or member 'DisposableToken.CachePermission.Role'

Check warning on line 7 in src/Momento.Sdk/Auth/AccessControl/DisposableToken.cs

View workflow job for this annotation

GitHub Actions / build_csharp (windows-latest, net461)

Missing XML comment for publicly visible type or member 'DisposableToken.CachePermission'
{
// public virtual bool Equals(CachePermission? other)
// {
// return false;
// }
}

public record CacheItemPermission
(CacheRole Role, CacheSelector CacheSelector, CacheItemSelector CacheItemSelector) : CachePermission(Role,
CacheSelector);

public record TopicPermission(TopicRole Role, CacheSelector CacheSelector, TopicSelector TopicSelector) : DisposableTokenPermission;
}
13 changes: 13 additions & 0 deletions src/Momento.Sdk/Auth/AccessControl/DisposableTokenScope.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.Collections.Generic;

namespace Momento.Sdk.Auth.AccessControl;

public class DisposableTokenScope
{
public List<DisposableTokenPermission> Permissions { get; }

public DisposableTokenScope(List<DisposableTokenPermission> Permissions)
{
this.Permissions = Permissions;
}
}
260 changes: 260 additions & 0 deletions src/Momento.Sdk/Auth/AccessControl/DisposableTokenScopes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,260 @@
using System.Collections.Generic;

namespace Momento.Sdk.Auth.AccessControl;

public record DisposableTokenScopes(List<DisposableTokenPermission> Permissions)
{
public static DisposableTokenScope CacheReadWrite(string cacheName)
{
return CacheReadWrite(CacheSelector.ByName(cacheName));
}

public static DisposableTokenScope CacheReadWrite(CacheSelector cacheSelector)
{
return new DisposableTokenScope(Permissions: new List<DisposableTokenPermission>
{
new DisposableToken.CacheItemPermission(
CacheRole.ReadWrite,
cacheSelector,
CacheItemSelector.AllCacheItems
)
});
}

public static DisposableTokenScope CacheReadOnly(string cacheName)
{
return CacheReadOnly(CacheSelector.ByName(cacheName));
}

public static DisposableTokenScope CacheReadOnly(CacheSelector cacheSelector)
{
return new DisposableTokenScope(Permissions: new List<DisposableTokenPermission>
{
new DisposableToken.CacheItemPermission(
CacheRole.ReadOnly,
cacheSelector,
CacheItemSelector.AllCacheItems
)
});
}

public static DisposableTokenScope CacheWriteOnly(string cacheName)
{
return CacheWriteOnly(CacheSelector.ByName(cacheName));
}

public static DisposableTokenScope CacheWriteOnly(CacheSelector cacheSelector)
{
return new DisposableTokenScope(Permissions: new List<DisposableTokenPermission>
{
new DisposableToken.CacheItemPermission(
CacheRole.WriteOnly,
cacheSelector,
CacheItemSelector.AllCacheItems
)
});
}


public static DisposableTokenScope CacheKeyReadWrite(string cacheName, string cacheKey)
{
return CacheKeyReadWrite(CacheSelector.ByName(cacheName), CacheItemSelector.ByKey(cacheKey));
}

public static DisposableTokenScope CacheKeyReadWrite(CacheSelector cacheSelector, string cacheKey)
{
return CacheKeyReadWrite(cacheSelector, CacheItemSelector.ByKey(cacheKey));
}

private static DisposableTokenScope CacheKeyReadWrite(CacheSelector cacheSelector, CacheItemSelector cacheItemSelector)
{
return new DisposableTokenScope(Permissions: new List<DisposableTokenPermission>
{
new DisposableToken.CacheItemPermission(
CacheRole.ReadWrite,
cacheSelector,
cacheItemSelector
)
});
}


public static DisposableTokenScope CacheKeyReadOnly(string cacheName, string cacheKey)
{
return CacheKeyReadOnly(CacheSelector.ByName(cacheName), CacheItemSelector.ByKey(cacheKey));
}

public static DisposableTokenScope CacheKeyReadOnly(CacheSelector cacheSelector, string cacheKey)
{
return CacheKeyReadOnly(cacheSelector, CacheItemSelector.ByKey(cacheKey));
}

private static DisposableTokenScope CacheKeyReadOnly(CacheSelector cacheSelector, CacheItemSelector cacheItemSelector)
{
return new DisposableTokenScope(Permissions: new List<DisposableTokenPermission>
{
new DisposableToken.CacheItemPermission(
CacheRole.ReadOnly,
cacheSelector,
cacheItemSelector
)
});
}

public static DisposableTokenScope CacheKeyWriteOnly(string cacheName, string cacheKey)
{
return CacheKeyWriteOnly(CacheSelector.ByName(cacheName), CacheItemSelector.ByKey(cacheKey));
}

public static DisposableTokenScope CacheKeyWriteOnly(CacheSelector cacheSelector, string cacheKey)
{
return CacheKeyWriteOnly(cacheSelector, CacheItemSelector.ByKey(cacheKey));
}

private static DisposableTokenScope CacheKeyWriteOnly(CacheSelector cacheSelector, CacheItemSelector cacheItemSelector)
{
return new DisposableTokenScope(Permissions: new List<DisposableTokenPermission>
{
new DisposableToken.CacheItemPermission(
CacheRole.WriteOnly,
cacheSelector,
cacheItemSelector
)
});
}


public static DisposableTokenScope CacheKeyPrefixReadWrite(string cacheName, string cacheKeyPrefix)
{
return CacheKeyPrefixReadWrite(CacheSelector.ByName(cacheName), CacheItemSelector.ByKeyPrefix(cacheKeyPrefix));
}

public static DisposableTokenScope CacheKeyPrefixReadWrite(CacheSelector cacheSelector, string cacheKeyPrefix)
{
return CacheKeyPrefixReadWrite(cacheSelector, CacheItemSelector.ByKeyPrefix(cacheKeyPrefix));
}

private static DisposableTokenScope CacheKeyPrefixReadWrite(CacheSelector cacheSelector, CacheItemSelector cacheItemSelector)
{
return new DisposableTokenScope(Permissions: new List<DisposableTokenPermission>
{
new DisposableToken.CacheItemPermission(
CacheRole.ReadWrite,
cacheSelector,
cacheItemSelector
)
});
}


public static DisposableTokenScope CacheKeyPrefixReadOnly(string cacheName, string cacheKeyPrefix)
{
return CacheKeyPrefixReadOnly(CacheSelector.ByName(cacheName), CacheItemSelector.ByKeyPrefix(cacheKeyPrefix));
}

public static DisposableTokenScope CacheKeyPrefixReadOnly(CacheSelector cacheSelector, string cacheKeyPrefix)
{
return CacheKeyPrefixReadOnly(cacheSelector, CacheItemSelector.ByKeyPrefix(cacheKeyPrefix));
}

private static DisposableTokenScope CacheKeyPrefixReadOnly(CacheSelector cacheSelector, CacheItemSelector cacheItemSelector)
{
return new DisposableTokenScope(Permissions: new List<DisposableTokenPermission>
{
new DisposableToken.CacheItemPermission(
CacheRole.ReadOnly,
cacheSelector,
cacheItemSelector
)
});
}

public static DisposableTokenScope CacheKeyPrefixWriteOnly(string cacheName, string cacheKeyPrefix)
{
return CacheKeyPrefixWriteOnly(CacheSelector.ByName(cacheName), CacheItemSelector.ByKeyPrefix(cacheKeyPrefix));
}

public static DisposableTokenScope CacheKeyPrefixWriteOnly(CacheSelector cacheSelector, string cacheKeyPrefix)
{
return CacheKeyPrefixWriteOnly(cacheSelector, CacheItemSelector.ByKeyPrefix(cacheKeyPrefix));
}

private static DisposableTokenScope CacheKeyPrefixWriteOnly(CacheSelector cacheSelector, CacheItemSelector cacheItemSelector)
{
return new DisposableTokenScope(Permissions: new List<DisposableTokenPermission>
{
new DisposableToken.CacheItemPermission(
CacheRole.WriteOnly,
cacheSelector,
cacheItemSelector
)
});
}

public static DisposableTokenScope TopicPublishSubscribe(string cacheName, string topicName)
{
return TopicPublishSubscribe(CacheSelector.ByName(cacheName), TopicSelector.ByName(topicName));
}

public static DisposableTokenScope TopicPublishSubscribe(CacheSelector cacheSelector, string topicName)
{
return TopicPublishSubscribe(cacheSelector, TopicSelector.ByName(topicName));
}

public static DisposableTokenScope TopicPublishSubscribe(CacheSelector cacheSelector, TopicSelector topicSelector)
{
return new DisposableTokenScope(Permissions: new List<DisposableTokenPermission>
{
new DisposableToken.TopicPermission(
TopicRole.PublishSubscribe,
cacheSelector,
topicSelector
)
});
}


public static DisposableTokenScope TopicSubscribeOnly(string cacheName, string topicName)
{
return TopicSubscribeOnly(CacheSelector.ByName(cacheName), TopicSelector.ByName(topicName));
}

public static DisposableTokenScope TopicSubscribeOnly(CacheSelector cacheSelector, string topicName)
{
return TopicSubscribeOnly(cacheSelector, TopicSelector.ByName(topicName));
}

public static DisposableTokenScope TopicSubscribeOnly(CacheSelector cacheSelector, TopicSelector topicSelector)
{
return new DisposableTokenScope(Permissions: new List<DisposableTokenPermission>
{
new DisposableToken.TopicPermission(
TopicRole.SubscribeOnly,
cacheSelector,
topicSelector
)
});
}

public static DisposableTokenScope TopicPublishOnly(string cacheName, string topicName)
{
return TopicPublishOnly(CacheSelector.ByName(cacheName), TopicSelector.ByName(topicName));
}

public static DisposableTokenScope TopicPublishOnly(CacheSelector cacheSelector, string topicName)
{
return TopicPublishOnly(cacheSelector, TopicSelector.ByName(topicName));
}

public static DisposableTokenScope TopicPublishOnly(CacheSelector cacheSelector, TopicSelector topicSelector)
{
return new DisposableTokenScope(Permissions: new List<DisposableTokenPermission>
{
new DisposableToken.TopicPermission(
TopicRole.PublishOnly,
cacheSelector,
topicSelector
)
});
}
}
92 changes: 92 additions & 0 deletions src/Momento.Sdk/Auth/AccessControl/ExpiresIn.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using System;


public abstract class Expiration
{
private readonly bool doesExpire;

protected Expiration(bool doesExpire){
this.doesExpire = doesExpire;
}

public bool DoesExpire()
{
return doesExpire;
}
}

public class ExpiresIn : Expiration
{
private readonly int? validForSeconds;

private ExpiresIn(int? validForSeconds) : base(validForSeconds != null)
{
this.validForSeconds = validForSeconds;
}

public int? Seconds()
{
return validForSeconds;
}

public static ExpiresIn Never() {
return new ExpiresIn(null);
}

public static ExpiresIn Seconds(int validForSeconds)
{
return new ExpiresIn(validForSeconds);
}

public static ExpiresIn Minutes(int validForSeconds)
{
return new ExpiresIn(validForSeconds * 60);
}

public static ExpiresIn Hours(int validForSeconds)
{
return new ExpiresIn(validForSeconds * 3600);
}

public static ExpiresIn Days(int validForSeconds)
{
return new ExpiresIn(validForSeconds * 86400);
}

public static ExpiresIn Epoch(ulong expiresIn)
{
ulong now = (ulong)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalSeconds;
return new ExpiresIn((int)(expiresIn - now));
}

public static ExpiresIn Epoch(int expiresIn) {
return Epoch((ulong)expiresIn);
}

}

public class ExpiresAt : Expiration
{
private readonly int? validUntil;

private ExpiresAt(int? epochTimestamp) : base(epochTimestamp != 0 && epochTimestamp != null) {
if (this.DoesExpire())
{
this.validUntil = epochTimestamp;
}
else
{
this.validUntil = null;
}
}

public int? Epoch()
{
return validUntil;
}

public static ExpiresAt FromEpoch(int? epoch)
{
return new ExpiresAt(epoch);
}
}
Loading

0 comments on commit b2fc063

Please sign in to comment.