Skip to content

Commit 4ac2e99

Browse files
committed
add sentry tag, landing page header "create your own link" change
1 parent f50bbce commit 4ac2e99

17 files changed

+161
-155
lines changed

pkg/auth/sessions.go

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func (dc *Client) SetCookie(tokenString string, w http.ResponseWriter) {
6565
http.SetCookie(w, &cookie)
6666
}
6767

68-
func ValidateToken(ctx context.Context, tokenString string) (bool, error) {
68+
func ValidateToken(tokenString string) (bool, error) {
6969

7070
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
7171
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
@@ -78,7 +78,7 @@ func ValidateToken(ctx context.Context, tokenString string) (bool, error) {
7878
if err != nil || !token.Valid {
7979
return false, err
8080
}
81-
return false, nil
81+
return true, nil
8282
}
8383

8484
func GetToken(r *http.Request) (string, error) {
@@ -97,12 +97,12 @@ func RetrieveTokenValue(field string, r *http.Request) (jwt.MapClaims, interface
9797
claims := jwt.MapClaims{}
9898
tokenFromCookie, err := r.Cookie(CookieName)
9999

100-
tokenString := tokenFromCookie.Value
101-
102100
if err != nil {
103101
return claims, "", fmt.Errorf("discordClient.RetrieveTokenValue: %w", err)
104102
}
105103

104+
tokenString := tokenFromCookie.Value
105+
106106
// Parse the JWT token string
107107
// FIXME: https://auth0.com/blog/critical-vulnerabilities-in-json-web-token-libraries/
108108
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
@@ -143,3 +143,23 @@ func WithSession(ctx context.Context, key contextKey, ctxValue string) context.C
143143

144144
return context.WithValue(ctx, key, ctxValue)
145145
}
146+
147+
func IsUser(r *http.Request) (bool, error) {
148+
149+
token, err := GetToken(r)
150+
151+
if err != nil {
152+
153+
return false, err
154+
}
155+
156+
result, err := ValidateToken(token)
157+
158+
if err != nil {
159+
160+
return false, err
161+
}
162+
163+
return result, nil
164+
165+
}

pkg/handler/index.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
11
package handler
22

33
import (
4+
"fmt"
45
"net/http"
56

7+
"github.com/zmzlois/LinkGoGo/pkg/auth"
68
"github.com/zmzlois/LinkGoGo/web/pages"
9+
"go.uber.org/zap"
710
)
811

912
func Index(w http.ResponseWriter, r *http.Request) {
10-
pages.HomePage().Render(r.Context(), w)
13+
14+
var isUser = false
15+
16+
isUser, err := auth.IsUser(r)
17+
18+
if err != nil {
19+
fmt.Printf("Index.isUser: %v", zap.Error(err))
20+
}
21+
22+
pages.HomePage(isUser).Render(r.Context(), w)
1123
}

pkg/handler/unauthorised.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package handler
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
7+
"github.com/zmzlois/LinkGoGo/pkg/utils"
8+
"github.com/zmzlois/LinkGoGo/web/pages"
9+
)
10+
11+
func UnauthorisedHandler(w http.ResponseWriter, r *http.Request) {
12+
13+
// how this string should looks like based on environment
14+
// <meta http-equiv="refresh" content="5; url=http://localhost:3000/login"/>
15+
var loginURL string
16+
17+
URL := utils.Config("URL")
18+
19+
loginURL = fmt.Sprintf("5; url=%s/login", URL)
20+
21+
pages.UnauthorisedPage(loginURL).Render(r.Context(), w)
22+
}

pkg/middleware/auth_middleware.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func AuthMiddleware(next http.Handler) http.Handler {
3131

3232
// Parse and validate JWT token
3333
ctx := r.Context()
34-
authenticated, err := auth.ValidateToken(ctx, tokenString)
34+
authenticated, err := auth.ValidateToken(tokenString)
3535
if err != nil {
3636

3737
fmt.Println("failed to validate session token", zap.Error(err))

pkg/middleware/redirect_middleware.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func RedirectMiddleware(next http.Handler) http.Handler {
2323
}
2424

2525
ctx := r.Context()
26-
authenticated, err := auth.ValidateToken(ctx, tokenString)
26+
authenticated, err := auth.ValidateToken(tokenString)
2727

2828
if err != nil {
2929
fmt.Printf("AuthMiddleware.ValidateToken: %v", zap.Error(err))

pkg/monitor/sentry.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/getsentry/sentry-go"
1111
sentryhttp "github.com/getsentry/sentry-go/http"
1212
"github.com/joho/godotenv"
13+
"github.com/zmzlois/LinkGoGo/pkg/utils"
1314
)
1415

1516
type handler struct{}
@@ -58,6 +59,7 @@ func SentryInit() {
5859

5960
return event
6061
},
62+
Environment: utils.Config("ENVIRONMENT"),
6163
Debug: true,
6264
AttachStacktrace: true,
6365
EnableTracing: true,

pkg/router/router.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,7 @@ func SetupRouter(app chi.Router) {
6060

6161
app.Get("/discord-callback", discordOAuthCallbackHandler)
6262

63-
app.Get("/unauthorised", func(w http.ResponseWriter, r *http.Request) {
64-
pages.UnauthorisedPage().Render(r.Context(), w)
65-
})
63+
app.Get("/unauthorised", handler.UnauthorisedHandler)
6664

6765
linkService := service.NewLinkService(db)
6866

web/pages/home.templ

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ var Links []model.NewLinkInput = []model.NewLinkInput{
2020
}
2121

2222
// }
23-
templ HomePage() {
23+
templ HomePage(isUser bool) {
2424
@shared.Page("Home") {
2525
@shared.Background() {
2626
@shared.Container() {
27-
@partials.Header()
27+
@partials.Header(isUser)
2828
}
2929
@shared.Container() {
3030
@partials.ProfileHeader(MockData["avatar"], MockData["name"], MockData["bio"])

web/pages/home_templ.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

web/pages/unauthorised.templ

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
package pages
22

3-
import "github.com/zmzlois/LinkGoGo/web/templates/shared"
3+
import (
4+
"github.com/zmzlois/LinkGoGo/web/templates/shared"
5+
)
46

5-
templ UnauthorisedPage() {
7+
templ UnauthorisedPage(loginURL string) {
68
@shared.Page("Unauthorized") {
79
<head>
8-
<meta http-equiv="refresh" content="5; url=http://localhost:3000"/>
10+
<meta http-equiv="refresh" content="5; url=http://localhost:3000/login"/>
911
</head>
1012
@shared.Background() {
1113
<div class="flex flex-col content-center items-center text-center justify-center py-10 gap-8">
1214
<h1 class="font-bold text-zinc-900 lg:text-4xl text-2xl">You are not authorised</h1>
1315
<p>
14-
We are redirecting you to home page.
16+
We are redirecting you to login.
1517
</p>
1618
<svg class="animate-spin -ml-1 mr-3 h-5 w-5 text-white" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24">
1719
<circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle>

0 commit comments

Comments
 (0)