Skip to content

Commit

Permalink
use argon2
Browse files Browse the repository at this point in the history
  • Loading branch information
alexferl committed Oct 6, 2023
1 parent a3b4bad commit 33db72e
Show file tree
Hide file tree
Showing 23 changed files with 347 additions and 393 deletions.
3 changes: 2 additions & 1 deletion casbin/policy.csv
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
p, any, /, GET
p, any, /healthz, GET
p, any, /readyz, GET
p, any, /livez, GET
p, any, /favicon.ico, GET
p, any, /docs, GET
p, any, /openapi/*, GET
Expand Down
45 changes: 5 additions & 40 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import (
"time"

libConfig "github.com/alexferl/golib/config"
libHttp "github.com/alexferl/golib/http/config"
libMongo "github.com/alexferl/golib/database/mongodb"
libHttp "github.com/alexferl/golib/http/api/config"
libLog "github.com/alexferl/golib/log"
"github.com/rs/zerolog/log"
"github.com/spf13/pflag"
Expand All @@ -17,6 +18,7 @@ type Config struct {
Config *libConfig.Config
HTTP *libHttp.Config
Logging *libLog.Config
MongoDB *libMongo.Config

BaseURL string

Expand All @@ -27,7 +29,6 @@ type Config struct {
CSRF *CSRF
Casbin *Casbin
OpenAPI *OpenAPI
MongoDB *MongoDB
}

type Admin struct {
Expand Down Expand Up @@ -73,22 +74,13 @@ type OpenAPI struct {
Schema string
}

type MongoDB struct {
URI string
Username string
Password string
ReplicaSet string
ServerSelectionTimeoutMs time.Duration
ConnectTimeoutMs time.Duration
SocketTimeoutMs time.Duration // query timeout
}

// New creates a Config instance
func New() *Config {
return &Config{
Config: libConfig.New("APP"),
HTTP: libHttp.DefaultConfig,
Logging: libLog.DefaultConfig,
MongoDB: libMongo.DefaultConfig,
BaseURL: "http://localhost:1323",
Admin: &Admin{
Create: false,
Expand Down Expand Up @@ -126,15 +118,6 @@ func New() *Config {
OpenAPI: &OpenAPI{
Schema: "./openapi/openapi.yaml",
},
MongoDB: &MongoDB{
URI: "mongodb://localhost:27017",
Username: "",
Password: "",
ReplicaSet: "",
ServerSelectionTimeoutMs: time.Millisecond * 5000,
ConnectTimeoutMs: time.Millisecond * 5000,
SocketTimeoutMs: time.Millisecond * 30000,
},
}
}

Expand Down Expand Up @@ -175,14 +158,6 @@ const (
CasbinPolicy = "casbin-policy"

OpenAPISchema = "openapi-schema"

MongoDBURI = "mongodb-uri"
MongoDBUsername = "mongodb-username"
MongoDBPassword = "mongodb-password"
MongoDBReplicaSet = "mongodb-replica-set"
MongoDBServerSelectionTimeoutMs = "mongodb-server-selection-timeout-ms"
MongoDBConnectTimeoutMs = "mongodb-connect-timeout-ms"
MongoDBSocketTimeoutMs = "mongodb-socket-timeout-ms"
)

// addFlags adds all the flags from the command line
Expand Down Expand Up @@ -221,17 +196,6 @@ func (c *Config) addFlags(fs *pflag.FlagSet) {
fs.StringVar(&c.Casbin.Policy, CasbinPolicy, c.Casbin.Policy, "Casbin policy file")

fs.StringVar(&c.OpenAPI.Schema, OpenAPISchema, c.OpenAPI.Schema, "OpenAPI schema file")

fs.StringVar(&c.MongoDB.URI, MongoDBURI, c.MongoDB.URI, "MongoDB URI")
fs.StringVar(&c.MongoDB.Username, MongoDBUsername, c.MongoDB.Username, "MongoDB username")
fs.StringVar(&c.MongoDB.Password, MongoDBPassword, c.MongoDB.Password, "MongoDB password")
fs.StringVar(&c.MongoDB.ReplicaSet, MongoDBReplicaSet, c.MongoDB.ReplicaSet, "MongoDB replica set")
fs.DurationVar(&c.MongoDB.ServerSelectionTimeoutMs, MongoDBServerSelectionTimeoutMs,
c.MongoDB.ServerSelectionTimeoutMs, "MongoDB server selection timeout ms")
fs.DurationVar(&c.MongoDB.ConnectTimeoutMs, MongoDBConnectTimeoutMs, c.MongoDB.ConnectTimeoutMs,
"MongoDB connect timeout ms")
fs.DurationVar(&c.MongoDB.SocketTimeoutMs, MongoDBSocketTimeoutMs, c.MongoDB.SocketTimeoutMs,
"MongoDB socket timeout ms")
}

func (c *Config) BindFlags() {
Expand All @@ -242,6 +206,7 @@ func (c *Config) BindFlags() {
c.addFlags(pflag.CommandLine)
c.Logging.BindFlags(pflag.CommandLine)
c.HTTP.BindFlags(pflag.CommandLine)
c.MongoDB.BindFlags(pflag.CommandLine)

err := c.Config.BindFlags()
if err != nil {
Expand Down
111 changes: 32 additions & 79 deletions data/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"time"

"github.com/alexferl/golib/database/mongodb"
"github.com/spf13/viper"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
Expand All @@ -12,76 +13,30 @@ import (
"github.com/alexferl/echo-boilerplate/config"
)

func NewClient() (*mongo.Client, error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

uri := viper.GetString(config.MongoDBURI)
if uri == "" {
uri = "mongodb://localhost:27017"
}

opts := options.Client()
opts.ApplyURI(uri)
opts.SetAppName(viper.GetString(config.AppName))
opts.SetServerSelectionTimeout(viper.GetDuration(config.MongoDBServerSelectionTimeoutMs))
opts.SetConnectTimeout(viper.GetDuration(config.MongoDBConnectTimeoutMs))
opts.SetSocketTimeout(viper.GetDuration(config.MongoDBSocketTimeoutMs))

username := viper.GetString(config.MongoDBUsername)
password := viper.GetString(config.MongoDBPassword)
if username != "" {
opts.SetAuth(options.Credential{
Username: username,
Password: password,
})
}

replSet := viper.GetString(config.MongoDBReplicaSet)
if replSet != "" {
opts.SetReplicaSet(replSet)
}

client, err := mongo.Connect(ctx, opts)
if err != nil {
return nil, err
}

return client, nil
}

func CreateIndexes(client *mongo.Client) {
db := client.Database(viper.GetString(config.AppName))

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

idxOpts := options.Index().
SetUnique(true).
SetCollation(&options.Collation{Locale: "en", Strength: 2})

usernameOpts := idxOpts.SetName("username")
indexModel := mongo.IndexModel{
Keys: bson.D{{"username", 1}},
Options: usernameOpts,
}
_, err := db.Collection("users").Indexes().CreateOne(ctx, indexModel)
if err != nil {
panic(err)
}

emailOpts := idxOpts.SetName("email")
indexModel = mongo.IndexModel{
Keys: bson.D{{"email", 1}},
Options: emailOpts,
}
_, err = db.Collection("users").Indexes().CreateOne(ctx, indexModel)
if err != nil {
panic(err)
}
func CreateIndexes(client *mongo.Client) error {
indexes := map[string][]mongo.IndexModel{}

username := "username"
email := "email"
t := true
_, err = db.Collection("users").Indexes().CreateMany(ctx, []mongo.IndexModel{

indexes["users"] = []mongo.IndexModel{
{
Keys: bson.D{{"username", 1}},
Options: &options.IndexOptions{
Name: &username,
Unique: &t,
Collation: &options.Collation{Locale: "en", Strength: 2},
},
},
{
Keys: bson.D{{"email", 1}},
Options: &options.IndexOptions{
Name: &email,
Unique: &t,
Collation: &options.Collation{Locale: "en", Strength: 2},
},
},
{
Keys: bson.D{
{"id", 1},
Expand All @@ -90,12 +45,9 @@ func CreateIndexes(client *mongo.Client) {
Unique: &t,
},
},
})
if err != nil {
panic(err)
}

_, err = db.Collection("tasks").Indexes().CreateMany(ctx, []mongo.IndexModel{
indexes["tasks"] = []mongo.IndexModel{
{
Keys: bson.D{
{"id", 1},
Expand All @@ -104,12 +56,9 @@ func CreateIndexes(client *mongo.Client) {
Unique: &t,
},
},
})
if err != nil {
panic(err)
}

_, err = db.Collection("personal_access_tokens").Indexes().CreateMany(ctx, []mongo.IndexModel{
indexes["personal_access_tokens"] = []mongo.IndexModel{
{
Keys: bson.D{
{"id", 1},
Expand All @@ -132,8 +81,12 @@ func CreateIndexes(client *mongo.Client) {
Unique: &t,
},
},
})
if err != nil {
panic(err)
}

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

db := client.Database(viper.GetString(config.AppName))

return mongodb.CreateIndexes(ctx, db, indexes)
}
95 changes: 49 additions & 46 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,83 +1,86 @@
module github.com/alexferl/echo-boilerplate

go 1.19
go 1.21

require (
github.com/alexferl/echo-casbin v0.3.0
github.com/alexferl/echo-jwt v0.6.0
github.com/alexferl/echo-openapi v0.2.0
github.com/alexferl/golib/config v0.0.0-20221113053029-e02f34f7806e
github.com/alexferl/golib/http v0.0.0-20221121203058-dd1292e95d4a
github.com/alexferl/golib/log v0.0.0-20221113053029-e02f34f7806e
github.com/alexferl/golib/config v0.0.0-20231006035358-2d73e8a9c73c
github.com/alexferl/golib/database/mongodb v0.0.0-20231006035358-2d73e8a9c73c
github.com/alexferl/golib/http/api v0.0.0-20231006035358-2d73e8a9c73c
github.com/alexferl/golib/log v0.0.0-20231006035358-2d73e8a9c73c
github.com/alexferl/httplink v0.1.0
github.com/casbin/casbin/v2 v2.57.0
github.com/labstack/echo/v4 v4.9.1
github.com/lestrrat-go/jwx/v2 v2.0.7
github.com/rs/xid v1.4.0
github.com/rs/zerolog v1.28.0
github.com/casbin/casbin/v2 v2.77.2
github.com/labstack/echo/v4 v4.11.1
github.com/lestrrat-go/jwx/v2 v2.0.13
github.com/matthewhartstonge/argon2 v0.3.4
github.com/rs/xid v1.5.0
github.com/rs/zerolog v1.31.0
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.14.0
github.com/stretchr/testify v1.8.1
go.mongodb.org/mongo-driver v1.11.0
golang.org/x/crypto v0.3.0
golang.org/x/exp v0.0.0-20221111204811-129d8d6c17ab
golang.org/x/oauth2 v0.2.0
github.com/spf13/viper v1.16.0
github.com/stretchr/testify v1.8.4
go.mongodb.org/mongo-driver v1.12.1
golang.org/x/exp v0.0.0-20231005195138-3e424a577f31
golang.org/x/oauth2 v0.12.0
)

require (
cloud.google.com/go/compute v1.12.1 // indirect
cloud.google.com/go/compute/metadata v0.2.1 // indirect
cloud.google.com/go/compute v1.20.1 // indirect
cloud.google.com/go/compute/metadata v0.2.3 // indirect
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.1.0 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/getkin/kin-openapi v0.108.0 // indirect
github.com/getkin/kin-openapi v0.107.0 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/swag v0.22.3 // indirect
github.com/goccy/go-json v0.9.11 // indirect
github.com/go-openapi/swag v0.19.5 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/snappy v0.0.1 // indirect
github.com/gorilla/mux v1.8.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/invopop/yaml v0.2.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/klauspost/compress v1.15.12 // indirect
github.com/invopop/yaml v0.1.0 // indirect
github.com/klauspost/compress v1.13.6 // indirect
github.com/labstack/gommon v0.4.0 // indirect
github.com/lestrrat-go/blackmagic v1.0.1 // indirect
github.com/lestrrat-go/blackmagic v1.0.2 // indirect
github.com/lestrrat-go/httpcc v1.0.1 // indirect
github.com/lestrrat-go/httprc v1.0.4 // indirect
github.com/lestrrat-go/iter v1.0.2 // indirect
github.com/lestrrat-go/option v1.0.0 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/lestrrat-go/option v1.0.1 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
github.com/montanaflynn/stats v0.6.6 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.5 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/afero v1.9.2 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/segmentio/asm v1.2.0 // indirect
github.com/spf13/afero v1.9.5 // indirect
github.com/spf13/cast v1.5.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/subosito/gotenv v1.4.1 // indirect
github.com/subosito/gotenv v1.4.2 // indirect
github.com/tidwall/gjson v1.14.4 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.2 // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
github.com/xdg-go/scram v1.1.1 // indirect
github.com/xdg-go/stringprep v1.0.3 // indirect
github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a // indirect
golang.org/x/net v0.2.0 // indirect
github.com/xdg-go/scram v1.1.2 // indirect
github.com/xdg-go/stringprep v1.0.4 // indirect
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect
golang.org/x/crypto v0.13.0 // indirect
golang.org/x/net v0.15.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.2.0 // indirect
golang.org/x/text v0.4.0 // indirect
golang.org/x/time v0.2.0 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/time v0.3.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.28.1 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
Loading

0 comments on commit 33db72e

Please sign in to comment.