Skip to content

Commit f55c68c

Browse files
authored
Fixes (#9)
1 parent 31943b5 commit f55c68c

File tree

7 files changed

+38
-14
lines changed

7 files changed

+38
-14
lines changed

.vscode/launch.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
"name": "Launch Package",
6+
"type": "go",
7+
"request": "launch",
8+
"mode": "debug",
9+
"program": "${workspaceFolder}/cmd/server",
10+
"args": [
11+
"--config=${workspaceFolder}/config/config.yaml"
12+
]
13+
}
14+
]
15+
}

cmd/server/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func main() {
6262

6363
addr := ":8080"
6464
logger.Infof("Listening on %s...", addr)
65-
if err := http.Start(ctx, addr, &conf.Server, pub, store, svc, client); err != nil {
65+
if err := http.Start(ctx, addr, conf, pub, store, svc, client); err != nil {
6666
logger.Fatalf("http server failed: %v", err)
6767
}
6868
}

pkg/api/http/api.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ import (
1616
)
1717

1818
// Start starts the server with specified store.
19-
func Start(ctx context.Context, addr string, conf *config.Server, pub eventapi.Publisher, store domain.Store, svc domain.UserService, checker domain.SessionChecker) error {
20-
s := newServer(pub, store, svc, checker)
19+
func Start(ctx context.Context, addr string, conf *config.Settings, pub eventapi.Publisher, store domain.Store, svc domain.UserService, checker domain.SessionChecker) error {
20+
s := newServer(pub, store, svc, checker, &conf.Kratos)
2121

2222
srv := http.Server{
2323
Addr: addr,
2424
Handler: handlers.CustomLoggingHandler(os.Stdout, handlers.ProxyHeaders(s), logFormatter),
25-
ReadTimeout: conf.ReadTimeout.Duration,
26-
WriteTimeout: conf.WriteTimeout.Duration,
27-
IdleTimeout: conf.IdleTimeout.Duration,
25+
ReadTimeout: conf.Server.ReadTimeout.Duration,
26+
WriteTimeout: conf.Server.WriteTimeout.Duration,
27+
IdleTimeout: conf.Server.IdleTimeout.Duration,
2828
MaxHeaderBytes: 1 << 20,
2929
}
3030

@@ -37,7 +37,7 @@ func Start(ctx context.Context, addr string, conf *config.Server, pub eventapi.P
3737
case err := <-errs:
3838
return err
3939
case <-ctx.Done():
40-
ctxShutDown, cancel := context.WithTimeout(context.Background(), conf.ShutdownTimeout.Duration)
40+
ctxShutDown, cancel := context.WithTimeout(context.Background(), conf.Server.ShutdownTimeout.Duration)
4141
defer cancel()
4242

4343
err := srv.Shutdown(ctxShutDown)

pkg/api/http/routes.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ import (
55

66
"github.com/gorilla/mux"
77

8+
"github.com/opencars/auth/pkg/config"
89
"github.com/opencars/auth/pkg/domain"
910
"github.com/opencars/auth/pkg/eventapi"
1011
)
1112

12-
func configureRouter(pub eventapi.Publisher, store domain.Store, svc domain.UserService, checker domain.SessionChecker) http.Handler {
13+
func configureRouter(pub eventapi.Publisher, store domain.Store, svc domain.UserService, checker domain.SessionChecker, conf *config.Kratos) http.Handler {
1314
tokens := tokenHandler{svc: svc}
1415
tAuth := AuthHandler{publisher: pub, store: store}
1516

@@ -22,7 +23,7 @@ func configureRouter(pub eventapi.Publisher, store domain.Store, svc domain.User
2223

2324
user := router.PathPrefix("/user").Subrouter()
2425
user.Use(
25-
SessionCheckerMiddleware(checker),
26+
SessionCheckerMiddleware(conf.Cookie, checker),
2627
)
2728

2829
user.Handle("/tokens", tokens.Create()).Methods("POST") // POST /api/v1/tokens.

pkg/api/http/server.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/gorilla/handlers"
99

10+
"github.com/opencars/auth/pkg/config"
1011
"github.com/opencars/auth/pkg/domain"
1112
"github.com/opencars/auth/pkg/eventapi"
1213
"github.com/opencars/auth/pkg/handler"
@@ -19,9 +20,9 @@ type server struct {
1920
store domain.Store
2021
}
2122

22-
func newServer(pub eventapi.Publisher, store domain.Store, svc domain.UserService, checker domain.SessionChecker) *server {
23+
func newServer(pub eventapi.Publisher, store domain.Store, svc domain.UserService, checker domain.SessionChecker, conf *config.Kratos) *server {
2324
s := server{
24-
router: configureRouter(pub, store, svc, checker),
25+
router: configureRouter(pub, store, svc, checker, conf),
2526
publisher: pub,
2627
store: store,
2728
}

pkg/api/http/user_middleware.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,19 @@ var (
1313
ErrUnauthorized = httputil.NewError(http.StatusUnauthorized, "user.not_authorized")
1414
)
1515

16-
func SessionCheckerMiddleware(checker domain.SessionChecker) mux.MiddlewareFunc {
16+
func SessionCheckerMiddleware(cookieName string, checker domain.SessionChecker) mux.MiddlewareFunc {
1717
return func(next http.Handler) http.Handler {
1818
return httputil.Handler(func(w http.ResponseWriter, r *http.Request) error {
19-
cookie := r.Header.Get("Cookie")
19+
var cookieValue string
20+
21+
cookie, err := r.Cookie(cookieName)
22+
if err == nil && cookie != nil {
23+
cookieValue = cookie.Value
24+
}
25+
2026
sessionToken := strings.Replace(r.Header.Get("Authorization"), "Bearer ", "", 1)
2127

22-
user, err := checker.CheckSession(r.Context(), sessionToken, cookie)
28+
user, err := checker.CheckSession(r.Context(), sessionToken, cookieValue)
2329
if err != nil {
2430
return ErrUnauthorized
2531
}

pkg/config/kratos.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ package config
22

33
type Kratos struct {
44
BaseURL string `yaml:"base_url"`
5+
Cookie string `yaml:"cookie"`
56
}

0 commit comments

Comments
 (0)