2022-01-21 06:48:07 +00:00
|
|
|
package arangodb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-02-06 12:44:19 +00:00
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
|
|
|
"encoding/base64"
|
|
|
|
"fmt"
|
2022-01-21 06:48:07 +00:00
|
|
|
|
|
|
|
arangoDriver "github.com/arangodb/go-driver"
|
|
|
|
"github.com/arangodb/go-driver/http"
|
|
|
|
"github.com/authorizerdev/authorizer/server/db/models"
|
2022-05-29 11:52:46 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/memorystore"
|
2022-01-21 06:48:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type provider struct {
|
|
|
|
db arangoDriver.Database
|
|
|
|
}
|
|
|
|
|
|
|
|
// for this we need arangodb instance up and running
|
|
|
|
// for local testing we can use dockerized version of it
|
|
|
|
// docker run -p 8529:8529 -e ARANGO_ROOT_PASSWORD=root arangodb/arangodb:3.8.4
|
|
|
|
|
|
|
|
// NewProvider to initialize arangodb connection
|
|
|
|
func NewProvider() (*provider, error) {
|
|
|
|
ctx := context.Background()
|
2022-05-31 02:44:03 +00:00
|
|
|
dbURL := memorystore.RequiredEnvStoreObj.GetRequiredEnv().DatabaseURL
|
2023-02-06 12:44:19 +00:00
|
|
|
dbUsername := memorystore.RequiredEnvStoreObj.GetRequiredEnv().DatabaseUsername
|
|
|
|
dbPassword := memorystore.RequiredEnvStoreObj.GetRequiredEnv().DatabasePassword
|
|
|
|
dbCACertificate := memorystore.RequiredEnvStoreObj.GetRequiredEnv().DatabaseCACert
|
|
|
|
httpConfig := http.ConnectionConfig{
|
2022-05-29 11:52:46 +00:00
|
|
|
Endpoints: []string{dbURL},
|
2023-02-06 12:44:19 +00:00
|
|
|
}
|
|
|
|
// If ca certificate if present, create tls config
|
|
|
|
if dbCACertificate != "" {
|
|
|
|
caCert, err := base64.StdEncoding.DecodeString(dbCACertificate)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Prepare TLS Config
|
|
|
|
tlsConfig := &tls.Config{}
|
|
|
|
certPool := x509.NewCertPool()
|
|
|
|
if success := certPool.AppendCertsFromPEM(caCert); !success {
|
|
|
|
return nil, fmt.Errorf("invalid certificate")
|
|
|
|
}
|
|
|
|
tlsConfig.RootCAs = certPool
|
|
|
|
httpConfig.TLSConfig = tlsConfig
|
|
|
|
}
|
|
|
|
// Create new http connection
|
|
|
|
conn, err := http.NewConnection(httpConfig)
|
2022-01-21 06:48:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-02-06 12:44:19 +00:00
|
|
|
clientConfig := arangoDriver.ClientConfig{
|
2022-01-21 06:48:07 +00:00
|
|
|
Connection: conn,
|
2023-02-06 12:44:19 +00:00
|
|
|
}
|
|
|
|
if dbUsername != "" && dbPassword != "" {
|
|
|
|
clientConfig.Authentication = arangoDriver.BasicAuthentication(dbUsername, dbPassword)
|
|
|
|
}
|
|
|
|
arangoClient, err := arangoDriver.NewClient(clientConfig)
|
2022-01-21 06:48:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-02-06 12:44:19 +00:00
|
|
|
var arangodb arangoDriver.Database
|
2022-05-31 02:44:03 +00:00
|
|
|
dbName := memorystore.RequiredEnvStoreObj.GetRequiredEnv().DatabaseName
|
2023-02-06 12:44:19 +00:00
|
|
|
arangodb_exists, err := arangoClient.DatabaseExists(ctx, dbName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-01-21 06:48:07 +00:00
|
|
|
if arangodb_exists {
|
2023-02-06 12:44:19 +00:00
|
|
|
arangodb, err = arangoClient.Database(ctx, dbName)
|
2022-01-21 06:48:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
2023-02-06 12:44:19 +00:00
|
|
|
arangodb, err = arangoClient.CreateDatabase(ctx, dbName, nil)
|
2022-01-21 06:48:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
userCollectionExists, err := arangodb.CollectionExists(ctx, models.Collections.User)
|
2023-02-06 12:44:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-05-12 19:17:01 +00:00
|
|
|
if !userCollectionExists {
|
2022-01-21 06:48:07 +00:00
|
|
|
_, err = arangodb.CreateCollection(ctx, models.Collections.User, nil)
|
|
|
|
if err != nil {
|
2022-05-12 19:17:01 +00:00
|
|
|
return nil, err
|
2022-01-21 06:48:07 +00:00
|
|
|
}
|
|
|
|
}
|
2023-02-06 12:44:19 +00:00
|
|
|
userCollection, err := arangodb.Collection(ctx, models.Collections.User)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-01-21 06:48:07 +00:00
|
|
|
userCollection.EnsureHashIndex(ctx, []string{"email"}, &arangoDriver.EnsureHashIndexOptions{
|
|
|
|
Unique: true,
|
|
|
|
Sparse: true,
|
|
|
|
})
|
|
|
|
userCollection.EnsureHashIndex(ctx, []string{"phone_number"}, &arangoDriver.EnsureHashIndexOptions{
|
|
|
|
Unique: true,
|
|
|
|
Sparse: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
verificationRequestCollectionExists, err := arangodb.CollectionExists(ctx, models.Collections.VerificationRequest)
|
2023-02-06 12:44:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-05-12 19:17:01 +00:00
|
|
|
if !verificationRequestCollectionExists {
|
2022-01-21 06:48:07 +00:00
|
|
|
_, err = arangodb.CreateCollection(ctx, models.Collections.VerificationRequest, nil)
|
|
|
|
if err != nil {
|
2022-05-12 19:17:01 +00:00
|
|
|
return nil, err
|
2022-01-21 06:48:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-06 12:44:19 +00:00
|
|
|
verificationRequestCollection, err := arangodb.Collection(ctx, models.Collections.VerificationRequest)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-01-21 06:48:07 +00:00
|
|
|
verificationRequestCollection.EnsureHashIndex(ctx, []string{"email", "identifier"}, &arangoDriver.EnsureHashIndexOptions{
|
|
|
|
Unique: true,
|
|
|
|
Sparse: true,
|
|
|
|
})
|
|
|
|
verificationRequestCollection.EnsureHashIndex(ctx, []string{"token"}, &arangoDriver.EnsureHashIndexOptions{
|
|
|
|
Sparse: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
sessionCollectionExists, err := arangodb.CollectionExists(ctx, models.Collections.Session)
|
2023-02-06 12:44:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-05-12 19:17:01 +00:00
|
|
|
if !sessionCollectionExists {
|
2022-01-21 06:48:07 +00:00
|
|
|
_, err = arangodb.CreateCollection(ctx, models.Collections.Session, nil)
|
|
|
|
if err != nil {
|
2022-05-12 19:17:01 +00:00
|
|
|
return nil, err
|
2022-01-21 06:48:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-06 12:44:19 +00:00
|
|
|
sessionCollection, err := arangodb.Collection(ctx, models.Collections.Session)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-01-21 06:48:07 +00:00
|
|
|
sessionCollection.EnsureHashIndex(ctx, []string{"user_id"}, &arangoDriver.EnsureHashIndexOptions{
|
|
|
|
Sparse: true,
|
|
|
|
})
|
|
|
|
|
2023-02-06 12:44:19 +00:00
|
|
|
envCollectionExists, err := arangodb.CollectionExists(ctx, models.Collections.Env)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !envCollectionExists {
|
2022-01-21 06:48:07 +00:00
|
|
|
_, err = arangodb.CreateCollection(ctx, models.Collections.Env, nil)
|
|
|
|
if err != nil {
|
2022-05-12 19:17:01 +00:00
|
|
|
return nil, err
|
2022-01-21 06:48:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-08 13:39:23 +00:00
|
|
|
webhookCollectionExists, err := arangodb.CollectionExists(ctx, models.Collections.Webhook)
|
2023-02-06 12:44:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-07-08 13:39:23 +00:00
|
|
|
if !webhookCollectionExists {
|
|
|
|
_, err = arangodb.CreateCollection(ctx, models.Collections.Webhook, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-06 12:44:19 +00:00
|
|
|
webhookCollection, err := arangodb.Collection(ctx, models.Collections.Webhook)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-07-08 13:39:23 +00:00
|
|
|
webhookCollection.EnsureHashIndex(ctx, []string{"event_name"}, &arangoDriver.EnsureHashIndexOptions{
|
|
|
|
Unique: true,
|
|
|
|
Sparse: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
webhookLogCollectionExists, err := arangodb.CollectionExists(ctx, models.Collections.WebhookLog)
|
2023-02-06 12:44:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-07-08 13:39:23 +00:00
|
|
|
if !webhookLogCollectionExists {
|
|
|
|
_, err = arangodb.CreateCollection(ctx, models.Collections.WebhookLog, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-06 12:44:19 +00:00
|
|
|
webhookLogCollection, err := arangodb.Collection(ctx, models.Collections.WebhookLog)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-07-08 13:39:23 +00:00
|
|
|
webhookLogCollection.EnsureHashIndex(ctx, []string{"webhook_id"}, &arangoDriver.EnsureHashIndexOptions{
|
2022-07-08 13:40:37 +00:00
|
|
|
Sparse: true,
|
2022-07-08 13:39:23 +00:00
|
|
|
})
|
|
|
|
|
2022-07-15 04:53:45 +00:00
|
|
|
emailTemplateCollectionExists, err := arangodb.CollectionExists(ctx, models.Collections.EmailTemplate)
|
2023-02-06 12:44:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-07-15 04:53:45 +00:00
|
|
|
if !emailTemplateCollectionExists {
|
|
|
|
_, err = arangodb.CreateCollection(ctx, models.Collections.EmailTemplate, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-06 12:44:19 +00:00
|
|
|
emailTemplateCollection, err := arangodb.Collection(ctx, models.Collections.EmailTemplate)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-07-15 04:53:45 +00:00
|
|
|
emailTemplateCollection.EnsureHashIndex(ctx, []string{"event_name"}, &arangoDriver.EnsureHashIndexOptions{
|
|
|
|
Unique: true,
|
|
|
|
Sparse: true,
|
|
|
|
})
|
|
|
|
|
2022-07-23 10:36:52 +00:00
|
|
|
otpCollectionExists, err := arangodb.CollectionExists(ctx, models.Collections.OTP)
|
2023-02-06 12:44:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-07-23 10:36:52 +00:00
|
|
|
if !otpCollectionExists {
|
|
|
|
_, err = arangodb.CreateCollection(ctx, models.Collections.OTP, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2023-02-06 12:44:19 +00:00
|
|
|
otpCollection, err := arangodb.Collection(ctx, models.Collections.OTP)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-07-23 10:36:52 +00:00
|
|
|
otpCollection.EnsureHashIndex(ctx, []string{"email"}, &arangoDriver.EnsureHashIndexOptions{
|
|
|
|
Unique: true,
|
|
|
|
Sparse: true,
|
|
|
|
})
|
2023-07-13 06:09:22 +00:00
|
|
|
otpCollection.EnsureHashIndex(ctx, []string{"phone_number"}, &arangoDriver.EnsureHashIndexOptions{
|
2023-07-12 05:54:13 +00:00
|
|
|
Unique: true,
|
|
|
|
Sparse: true,
|
|
|
|
})
|
2022-01-21 06:48:07 +00:00
|
|
|
return &provider{
|
|
|
|
db: arangodb,
|
|
|
|
}, err
|
|
|
|
}
|