2023-11-16 13:00:54 +00:00
|
|
|
package arangodb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
|
|
|
|
arangoDriver "github.com/arangodb/go-driver"
|
|
|
|
|
|
|
|
"github.com/authorizerdev/authorizer/server/db/models"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (p *provider) AddAuthenticator(ctx context.Context, authenticators *models.Authenticator) (*models.Authenticator, error) {
|
|
|
|
exists, _ := p.GetAuthenticatorDetailsByUserId(ctx, authenticators.UserID, authenticators.Method)
|
|
|
|
if exists != nil {
|
|
|
|
return authenticators, nil
|
|
|
|
}
|
|
|
|
if authenticators.ID == "" {
|
|
|
|
authenticators.ID = uuid.New().String()
|
|
|
|
}
|
|
|
|
|
|
|
|
authenticators.Key = authenticators.ID
|
|
|
|
authenticators.CreatedAt = time.Now().Unix()
|
|
|
|
authenticators.UpdatedAt = time.Now().Unix()
|
|
|
|
|
|
|
|
authenticatorsCollection, _ := p.db.Collection(ctx, models.Collections.Authenticators)
|
|
|
|
meta, err := authenticatorsCollection.CreateDocument(arangoDriver.WithOverwrite(ctx), authenticators)
|
|
|
|
if err != nil {
|
2024-04-02 09:55:11 +00:00
|
|
|
return nil, err
|
2023-11-16 13:00:54 +00:00
|
|
|
}
|
|
|
|
authenticators.Key = meta.Key
|
|
|
|
authenticators.ID = meta.ID.String()
|
|
|
|
|
|
|
|
return authenticators, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *provider) UpdateAuthenticator(ctx context.Context, authenticators *models.Authenticator) (*models.Authenticator, error) {
|
|
|
|
authenticators.UpdatedAt = time.Now().Unix()
|
|
|
|
|
|
|
|
collection, _ := p.db.Collection(ctx, models.Collections.Authenticators)
|
|
|
|
meta, err := collection.UpdateDocument(ctx, authenticators.Key, authenticators)
|
|
|
|
if err != nil {
|
2024-04-02 09:55:11 +00:00
|
|
|
return nil, err
|
2023-11-16 13:00:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
authenticators.Key = meta.Key
|
|
|
|
authenticators.ID = meta.ID.String()
|
|
|
|
return authenticators, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *provider) GetAuthenticatorDetailsByUserId(ctx context.Context, userId string, authenticatorType string) (*models.Authenticator, error) {
|
|
|
|
var authenticators *models.Authenticator
|
|
|
|
query := fmt.Sprintf("FOR d in %s FILTER d.user_id == @user_id AND d.method == @method LIMIT 1 RETURN d", models.Collections.Authenticators)
|
|
|
|
bindVars := map[string]interface{}{
|
|
|
|
"user_id": userId,
|
|
|
|
"method": authenticatorType,
|
|
|
|
}
|
|
|
|
cursor, err := p.db.Query(ctx, query, bindVars)
|
|
|
|
if err != nil {
|
2024-04-02 09:55:11 +00:00
|
|
|
return nil, err
|
2023-11-16 13:00:54 +00:00
|
|
|
}
|
|
|
|
defer cursor.Close()
|
|
|
|
for {
|
|
|
|
if !cursor.HasMore() {
|
|
|
|
if authenticators == nil {
|
|
|
|
return authenticators, fmt.Errorf("authenticator not found")
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
_, err := cursor.ReadDocument(ctx, &authenticators)
|
|
|
|
if err != nil {
|
2024-04-02 09:55:11 +00:00
|
|
|
return nil, err
|
2023-11-16 13:00:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return authenticators, nil
|
|
|
|
}
|