Wrap up split cookie auth

This commit is contained in:
Amarpreet Minhas 2019-10-05 22:35:14 -04:00
parent 0ce261d9bd
commit 681b0df9f5
5 changed files with 29 additions and 7 deletions

View file

@ -4,6 +4,7 @@ import (
"database/sql"
"encoding/json"
"fmt"
"git.minhas.io/asara/sudoscientist-go-backend/packages/middleware"
"git.minhas.io/asara/sudoscientist-go-backend/packages/users"
"github.com/badoux/checkmail"
"github.com/dgrijalva/jwt-go"
@ -54,7 +55,7 @@ func Routes() *chi.Mux {
r.Post("/signin", signin)
r.Post("/register", register)
r.Group(func(r chi.Router) {
r.Use(jwtauth.Verifier(TokenAuth))
r.Use(jwtauth.Verify(TokenAuth, auth_middleware.TokenFromSplitCookie))
r.Use(jwtauth.Authenticator)
r.Post("/refresh", refresh)
})

View file

@ -4,6 +4,7 @@ import (
"database/sql"
"encoding/json"
"fmt"
"git.minhas.io/asara/sudoscientist-go-backend/packages/middleware"
"github.com/go-chi/chi"
"github.com/go-chi/jwtauth"
"github.com/go-chi/render"
@ -79,13 +80,13 @@ func Init() {
func Routes() *chi.Mux {
r := chi.NewRouter()
r.Group(func(r chi.Router) {
r.Use(jwtauth.Verifier(TokenAuth))
r.Use(jwtauth.Verify(TokenAuth, auth_middleware.TokenFromSplitCookie))
r.Use(jwtauth.Authenticator)
r.Post("/", createBlogPost)
r.Patch("/by-id/{id}", updateBlogPostById)
r.Get("/by-slug/{slug}", getBlogPostBySlug)
})
r.Get("/", getBlogPosts)
r.Get("/by-slug/{slug}", getBlogPostBySlug)
r.Get("/by-id/{id}", getBlogPostById)
r.Get("/by-tag/{tag}", getBlogPostsByTag)
r.Get("/by-author/{author}", getBlogPostsByAuthor)

View file

@ -0,0 +1,19 @@
package auth_middleware
import (
"fmt"
"net/http"
)
func TokenFromSplitCookie(r *http.Request) string {
dataCookie, err := r.Cookie("DataCookie")
if err != nil {
return ""
}
signatureCookie, err := r.Cookie("SignatureCookie")
if err != nil {
return ""
}
cookie := dataCookie.Value + "." + signatureCookie.Value
return cookie
}

View file

@ -4,6 +4,7 @@ import (
"database/sql"
"encoding/json"
"fmt"
"git.minhas.io/asara/sudoscientist-go-backend/packages/middleware"
"github.com/go-chi/chi"
"github.com/go-chi/jwtauth"
"github.com/go-chi/render"
@ -36,7 +37,7 @@ func Init() {
func Routes() *chi.Mux {
r := chi.NewRouter()
r.Group(func(r chi.Router) {
r.Use(jwtauth.Verifier(TokenAuth))
r.Use(jwtauth.Verify(TokenAuth, auth_middleware.TokenFromSplitCookie))
r.Use(jwtauth.Authenticator)
r.Put("/{username}", updateUser)
})