Skip to content

Commit 332ce6e

Browse files
committed
including a nonce into app id to allow to a user to create multiple apps with the same email
1 parent bdb9440 commit 332ce6e

File tree

4 files changed

+18
-7
lines changed

4 files changed

+18
-7
lines changed

api/apps.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,13 @@ func generateApp(email string) (string, string, string, error) {
145145
return "", "", "", fmt.Errorf("email is required")
146146
}
147147
// hash email
148-
appId, err := helpers.Hash(email, helpers.AppIdSize)
148+
hEmail, err := helpers.Hash(email, helpers.EmailHashSize)
149149
if err != nil {
150150
return "", "", "", err
151151
}
152+
bAppNonce := helpers.RandBytes(helpers.AppNonceSize)
153+
hAppNonce := hex.EncodeToString(bAppNonce)
154+
appId := hEmail + hAppNonce
152155
// generate secret
153156
secret, hSecret, err := appSecret()
154157
if err != nil {

api/tokens.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,9 @@ func (s *Service) validAdminToken(token, rawSecret string) (string, bool) {
131131
if err != nil {
132132
return "", false
133133
}
134-
// the admin has the same id as the app (the hased email)
135-
if userId != appId {
134+
// the app id is composed by the admin user id hash and a nonce, so
135+
// the app id starts with the admin user id, check if so
136+
if !strings.HasPrefix(appId, userId) {
136137
return "", false
137138
}
138139
// check if the secret is valid

cmd/authapi/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
const (
1818
defaultHost = "0.0.0.0"
1919
defaultPort = 8080
20-
defaultDatabaseURI = "mongodb://localhost:27017"
20+
defaultDatabaseURI = "mongodb://admin:password@localhost:27017/"
2121
defaultDatabaseName = "simpleauth"
2222
defaultEmailAddr = ""
2323
defaultEmailPass = ""
@@ -89,7 +89,7 @@ func main() {
8989
MongoURI: c.dbURI,
9090
Database: c.dbName,
9191
}); err != nil {
92-
log.Fatalln("error initializing db: %w", err)
92+
log.Fatalf("error initializing db: %v", err)
9393
}
9494
// create the service
9595
service, err := api.New(context.Background(), db, &api.Config{

helpers/consts.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,15 @@ const (
3232
// value of 4 (bytes).
3333
UserIdSize = 4
3434
// AppIdSize constant is the size of the app id, which is an integer with a
35-
// value of 4 (bytes).
36-
AppIdSize = 4
35+
// value of 8 (bytes).
36+
AppIdSize = 8
37+
// EmailHashSize constant is the size of the email hash, which is an integer
38+
// with a value of 4 (bytes). The email hash is used to generate the user id
39+
// and the app id.
40+
EmailHashSize = 4
41+
// AppNonceSize constant is the size of the app nonce, which is an integer
42+
// with a value of 4 (bytes). The app nonce is used to generate the app id.
43+
AppNonceSize = 4
3744
// SecretSize constant is the size of the secret, which is an integer with a
3845
// value of 16 (bytes).
3946
SecretSize = 16

0 commit comments

Comments
 (0)