Skip to content

Commit

Permalink
NOISSUE - Invitation Service Auth Helper (absmach#28)
Browse files Browse the repository at this point in the history
* feat(auth): Add invitationkey

Add InvitationKey to auth key types and set expiration duration

* feat(auth): Add support for InvitationKey in Identify function

This commit adds support for the InvitationKey type in the Identify function of the auth service. Now, when the key type is either RecoveryKey, AccessKey, or InvitationKey, the key's subject will be returned. This allows for identifying users with an invitation key.

Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com>

---------

Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com>
  • Loading branch information
rodneyosodo authored Nov 14, 2023
1 parent 4b5f06b commit 41176e8
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
3 changes: 2 additions & 1 deletion auth/api/grpc/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ type issueReq struct {
func (req issueReq) validate() error {
if req.keyType != auth.AccessKey &&
req.keyType != auth.APIKey &&
req.keyType != auth.RecoveryKey {
req.keyType != auth.RecoveryKey &&
req.keyType != auth.InvitationKey {
return apiutil.ErrInvalidAuthKey
}

Expand Down
2 changes: 2 additions & 0 deletions auth/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ const (
RecoveryKey
// APIKey enables the one to act on behalf of the user.
APIKey
// InvitationKey is a key for inviting new users.
InvitationKey
)

func (kt KeyType) String() string {
Expand Down
15 changes: 10 additions & 5 deletions auth/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ import (
)

const (
recoveryDuration = 5 * time.Minute
thingsKind = "things"
channelsKind = "channels"
usersKind = "users"
recoveryDuration = 5 * time.Minute
invitationDuration = 24 * time.Hour

thingsKind = "things"
channelsKind = "channels"
usersKind = "users"

thingType = "thing"
channelType = "channel"
Expand Down Expand Up @@ -124,6 +126,8 @@ func (svc service) Issue(ctx context.Context, token string, key Key) (Token, err
return svc.refreshKey(ctx, token, key)
case RecoveryKey:
return svc.tmpKey(recoveryDuration, key)
case InvitationKey:
return svc.tmpKey(invitationDuration, key)
default:
return svc.accessKey(key)
}
Expand Down Expand Up @@ -160,7 +164,7 @@ func (svc service) Identify(ctx context.Context, token string) (string, error) {
}

switch key.Type {
case RecoveryKey, AccessKey:
case RecoveryKey, AccessKey, InvitationKey:
return key.Subject, nil
case APIKey:
_, err := svc.keys.Retrieve(ctx, key.Issuer, key.ID)
Expand Down Expand Up @@ -312,6 +316,7 @@ func (svc service) CountSubjects(ctx context.Context, pr PolicyReq) (int, error)
}

func (svc service) tmpKey(duration time.Duration, key Key) (Token, error) {
key.ExpiresAt = time.Now().Add(duration)
value, err := svc.tokenizer.Issue(key)
if err != nil {
return Token{}, errors.Wrap(errIssueTmp, err)
Expand Down

0 comments on commit 41176e8

Please sign in to comment.