Skip to content

Commit 40cb0d1

Browse files
Merge pull request #33 from memphisdev/user-pass-based-auth
add user-pass-based-auth
2 parents b7ad9ea + f64709f commit 40cb0d1

File tree

7 files changed

+61
-43
lines changed

7 files changed

+61
-43
lines changed

.vscode/launch.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
"CLIENT_CERT_PATH": "",
1919
"CLIENT_KEY_PATH": "",
2020
"ROOT_CA_PATH": "",
21+
"USER_PASS_BASED_AUTH": "true",
22+
"ROOT_PASSWORD": "memphis"
2123
}
2224
}
2325
]

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ curl --location --request POST 'rest_gateway:4444/auth/authenticate' \
9696
--data-raw '{
9797
"username": "root",
9898
"connection_token": "memphis",
99+
"password": "memphis, // connect with only one of the following methods: connection token / password
99100
"token_expiry_in_minutes": 60,
100101
"refresh_token_expiry_in_minutes": 10000092
101102
}'

conf/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ type Configuration struct {
1818
CLIENT_CERT_PATH string
1919
CLIENT_KEY_PATH string
2020
ROOT_CA_PATH string
21+
USER_PASS_BASED_AUTH bool
22+
ROOT_PASSWORD string
2123
}
2224

2325
func GetConfig() Configuration {

handlers/auth.go

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,18 @@ func (ah AuthHandler) Authenticate(c *fiber.Ctx) error {
2626
"message": err.Error(),
2727
})
2828
}
29+
if body.ConnectionToken != "" && body.Password != "" {
30+
log.Errorf("Authenticate: You have to connect with only one of the following methods: connection token / password")
31+
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
32+
"message": "You have to connect with only one of the following methods: connection token / password",
33+
})
34+
}
35+
if body.ConnectionToken == "" && body.Password == "" {
36+
log.Errorf("Authenticate: You have to connect with one of the following methods: connection token / password")
37+
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
38+
"message": "You have to connect with one of the following methods: connection token / password",
39+
})
40+
}
2941
if err := utils.Validate(body); err != nil {
3042
return c.Status(400).JSON(fiber.Map{
3143
"message": err,
@@ -34,16 +46,16 @@ func (ah AuthHandler) Authenticate(c *fiber.Ctx) error {
3446

3547
var conn *memphis.Conn
3648
var err error
37-
if configuration.CLIENT_CERT_PATH != "" && configuration.CLIENT_KEY_PATH != "" && configuration.ROOT_CA_PATH != "" {
38-
conn, err = memphis.Connect(
39-
configuration.MEMPHIS_HOST,
40-
body.Username,
41-
body.ConnectionToken,
42-
memphis.Tls(configuration.CLIENT_CERT_PATH, configuration.CLIENT_KEY_PATH, configuration.ROOT_CA_PATH),
43-
)
49+
opts := []memphis.Option{memphis.Reconnect(true), memphis.MaxReconnect(10), memphis.ReconnectInterval(3 * time.Second)}
50+
if configuration.USER_PASS_BASED_AUTH {
51+
opts = append(opts, memphis.Password(body.Password))
4452
} else {
45-
conn, err = memphis.Connect(configuration.MEMPHIS_HOST, body.Username, body.ConnectionToken)
53+
opts = append(opts, memphis.ConnectionToken(body.ConnectionToken))
54+
}
55+
if configuration.CLIENT_CERT_PATH != "" && configuration.CLIENT_KEY_PATH != "" && configuration.ROOT_CA_PATH != "" {
56+
opts = append(opts, memphis.Tls(configuration.CLIENT_CERT_PATH, configuration.CLIENT_KEY_PATH, configuration.ROOT_CA_PATH))
4657
}
58+
conn, err = memphis.Connect(configuration.MEMPHIS_HOST, body.Username, opts...)
4759

4860
if err != nil {
4961
if strings.Contains(err.Error(), "Authorization Violation") {

logger/log.go

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,29 @@ import (
44
"crypto/tls"
55
"crypto/x509"
66
"fmt"
7-
"rest-gateway/conf"
87
"io/ioutil"
98
"log"
109
"os"
10+
"rest-gateway/conf"
1111
"time"
1212

1313
"github.com/gofiber/fiber/v2"
1414
"github.com/nats-io/nats.go"
1515
)
1616

1717
const (
18-
restGwSourceName = "rest-gateway"
19-
syslogsStreamName = "$memphis_syslogs"
20-
syslogsInfoSubject = "extern.info"
21-
syslogsWarnSubject = "extern.warn"
22-
syslogsErrSubject = "extern.err"
23-
labelLen = 3
24-
infoLabel = "[INF] "
25-
debugLabel = "[DBG] "
26-
warnLabel = "[WRN] "
27-
errorLabel = "[ERR] "
28-
fatalLabel = "[FTL] "
29-
traceLabel = "[TRC] "
18+
restGwSourceName = "rest-gateway"
19+
syslogsStreamName = "$memphis_syslogs"
20+
syslogsInfoSubject = "extern.info"
21+
syslogsWarnSubject = "extern.warn"
22+
syslogsErrSubject = "extern.err"
23+
labelLen = 3
24+
infoLabel = "[INF] "
25+
debugLabel = "[DBG] "
26+
warnLabel = "[WRN] "
27+
errorLabel = "[ERR] "
28+
fatalLabel = "[FTL] "
29+
traceLabel = "[TRC] "
3030
)
3131

3232
type streamWriter struct {
@@ -58,7 +58,7 @@ func (sw streamWriter) Write(p []byte) (int, error) {
5858
return len(p), nil
5959
}
6060

61-
func CreateLogger(hostname string, username string, token string) (*Logger, error) {
61+
func CreateLogger(hostname string, username string, creds string) (*Logger, error) {
6262
configuration := conf.GetConfig()
6363
var nc *nats.Conn
6464
var err error
@@ -68,10 +68,16 @@ func CreateLogger(hostname string, username string, token string) (*Logger, erro
6868
AllowReconnect: true,
6969
MaxReconnect: 10,
7070
ReconnectWait: 3 * time.Second,
71-
Token: username + "::" + token,
7271
Name: "MEMPHIS HTTP LOGGER",
7372
}
7473

74+
if configuration.USER_PASS_BASED_AUTH {
75+
natsOpts.Password = creds
76+
natsOpts.User = username
77+
} else {
78+
natsOpts.Token = username + "::" + creds
79+
}
80+
7581
if configuration.CLIENT_CERT_PATH != "" && configuration.CLIENT_KEY_PATH != "" && configuration.ROOT_CA_PATH != "" {
7682
cert, err := tls.LoadX509KeyPair(configuration.CLIENT_CERT_PATH, configuration.CLIENT_KEY_PATH)
7783
if err != nil {

main.go

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,16 @@ func main() {
1818
select {
1919
case <-ticker.C:
2020
var err error
21-
if configuration.CLIENT_CERT_PATH != "" && configuration.CLIENT_KEY_PATH != "" && configuration.ROOT_CA_PATH != "" {
22-
conn, err = memphis.Connect(
23-
configuration.MEMPHIS_HOST,
24-
configuration.ROOT_USER,
25-
configuration.CONNECTION_TOKEN,
26-
memphis.Reconnect(true),
27-
memphis.MaxReconnect(10),
28-
memphis.ReconnectInterval(3*time.Second),
29-
memphis.Tls(configuration.CLIENT_CERT_PATH, configuration.CLIENT_KEY_PATH, configuration.ROOT_CA_PATH),
30-
)
21+
opts := []memphis.Option{memphis.Reconnect(true), memphis.MaxReconnect(10), memphis.ReconnectInterval(3 * time.Second)}
22+
if configuration.USER_PASS_BASED_AUTH {
23+
opts = append(opts, memphis.Password(configuration.ROOT_PASSWORD))
3124
} else {
32-
conn, err = memphis.Connect(
33-
configuration.MEMPHIS_HOST,
34-
configuration.ROOT_USER,
35-
configuration.CONNECTION_TOKEN,
36-
memphis.Reconnect(true),
37-
memphis.MaxReconnect(10),
38-
memphis.ReconnectInterval(3*time.Second),
39-
)
25+
opts = append(opts, memphis.ConnectionToken(configuration.CONNECTION_TOKEN))
26+
}
27+
if configuration.CLIENT_CERT_PATH != "" && configuration.CLIENT_KEY_PATH != "" && configuration.ROOT_CA_PATH != "" {
28+
opts = append(opts, memphis.Tls(configuration.CLIENT_CERT_PATH, configuration.CLIENT_KEY_PATH, configuration.ROOT_CA_PATH))
4029
}
30+
conn, err = memphis.Connect(configuration.MEMPHIS_HOST, configuration.ROOT_USER, opts...)
4131
if err == nil {
4232
ticker.Stop()
4333
goto serverInit
@@ -48,7 +38,11 @@ func main() {
4838
}
4939

5040
serverInit:
51-
l, err := logger.CreateLogger(configuration.MEMPHIS_HOST, configuration.ROOT_USER, configuration.CONNECTION_TOKEN)
41+
creds := configuration.CONNECTION_TOKEN
42+
if configuration.USER_PASS_BASED_AUTH {
43+
creds = configuration.ROOT_PASSWORD
44+
}
45+
l, err := logger.CreateLogger(configuration.MEMPHIS_HOST, configuration.ROOT_USER, creds)
5246
if err != nil {
5347
panic("Logger creation failed - " + err.Error())
5448
}

models/auth.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ package models
22

33
type AuthSchema struct {
44
Username string `json:"username" validate:"required"`
5-
ConnectionToken string `json:"connection_token" validate:"required"`
5+
ConnectionToken string `json:"connection_token"`
6+
Password string `json:"password"`
67
TokenExpiryMins int `json:"token_expiry_in_minutes"`
78
RefreshTokenExpiryMins int `json:"refresh_token_expiry_in_minutes"`
89
}

0 commit comments

Comments
 (0)