Skip to content

Commit

Permalink
append token to authorization Header
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthieu Jacquot committed Aug 3, 2018
1 parent 715be49 commit 2a83b05
Show file tree
Hide file tree
Showing 16 changed files with 64 additions and 74 deletions.
28 changes: 24 additions & 4 deletions implem/gin.server/ROUTER.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import (
"fmt"
"net/http"

"strings"

"errors"

"github.com/err0r500/go-realworld-clean/uc"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/render"
Expand Down Expand Up @@ -53,9 +57,10 @@ func (rH RouterHandler) usersRoutes(api *gin.RouterGroup) {
users.POST("", rH.userPost) // Register a new user
users.POST("/login", rH.userLoginPost) // Login for existing user

users.GET("", rH.jwtMiddleware(), rH.userGet) // Gets the currently logged-in user
users.PUT("", rH.jwtMiddleware(), rH.userPatch) // WARNING : it's a in fact a PATCH request in the API contract !!!
users.PATCH("", rH.jwtMiddleware(), rH.userPatch) // just in case it's fixed one day....
user := api.Group("/user")
user.GET("", rH.jwtMiddleware(), rH.userGet) // Gets the currently logged-in user
user.PUT("", rH.jwtMiddleware(), rH.userPatch) // WARNING : it's a in fact a PATCH request in the API contract !!!
user.PATCH("", rH.jwtMiddleware(), rH.userPatch) // just in case it's fixed one day....
}

func (rH RouterHandler) articlesRoutes(api *gin.RouterGroup) {
Expand Down Expand Up @@ -87,7 +92,14 @@ const userNameKey = "userNameKey"

func (rH RouterHandler) jwtMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
userName, err := rH.authHandler.GetUserName(c.GetHeader("Authorization"))
jwt, err := getJWT(c.GetHeader("Authorization"))
if err != nil {
c.Status(http.StatusUnauthorized)
c.Abort()
return
}

userName, err := rH.authHandler.GetUserName(jwt)
if err != nil {
c.Status(http.StatusUnauthorized)
c.Abort()
Expand All @@ -99,6 +111,14 @@ func (rH RouterHandler) jwtMiddleware() gin.HandlerFunc {
}
}

func getJWT(authHeader string) (string, error) {
splitted := strings.Split(authHeader, "Token ")
if len(splitted) != 2 {
return "", errors.New("malformed header")
}
return splitted[1], nil
}

func (rH RouterHandler) errorCatcher() gin.HandlerFunc {
return func(c *gin.Context) {
c.Next()
Expand Down
8 changes: 4 additions & 4 deletions implem/gin.server/articleComment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestArticleCommentGet(t *testing.T) {
t.Run("happyCase", func(t *testing.T) {
baloo.New(ts.URL).
Get(articleCommentPath).
AddHeader("Authorization", authToken).
AddHeader("Authorization", testData.TokenPrefix+authToken).
Expect(t).
Status(200).
JSONSchema(testData.CommentsMultipleResponse).
Expand Down Expand Up @@ -86,7 +86,7 @@ func TestArticleCommentPost(t *testing.T) {
t.Run("happyCase", func(t *testing.T) {
baloo.New(ts.URL).
Post(articleCommentPath).
AddHeader("Authorization", authToken).
AddHeader("Authorization", testData.TokenPrefix+authToken).
BodyString(validReq).
Expect(t).
Status(201).
Expand All @@ -107,7 +107,7 @@ func TestArticleCommentPost(t *testing.T) {
t.Run("no body", func(t *testing.T) {
baloo.New(ts.URL).
Post(articleCommentPath).
AddHeader("Authorization", authToken).
AddHeader("Authorization", testData.TokenPrefix+authToken).
Expect(t).
Status(400).
Done()
Expand Down Expand Up @@ -141,7 +141,7 @@ func TestArticleCommentDelete(t *testing.T) {
t.Run("happyCase", func(t *testing.T) {
baloo.New(ts.URL).
Delete(articleCommentPath+"/"+strconv.Itoa(testData.Article("jane").Comments[0].ID)).
AddHeader("Authorization", authToken).
AddHeader("Authorization", testData.TokenPrefix+authToken).
Expect(t).
Status(200).
Done()
Expand Down
4 changes: 2 additions & 2 deletions implem/gin.server/articleFavorite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestArticleFavoritePost(t *testing.T) {
t.Run("happyCase Add to favorites", func(t *testing.T) {
baloo.New(ts.URL).
Post(articleFavoritePath).
AddHeader("Authorization", authToken).
AddHeader("Authorization", testData.TokenPrefix+authToken).
Expect(t).
Status(200).
Done()
Expand Down Expand Up @@ -88,7 +88,7 @@ func TestArticleFavoriteDelete(t *testing.T) {
t.Run("happyCase Remove from to favorites", func(t *testing.T) {
baloo.New(ts.URL).
Delete(articleFavoritePath).
AddHeader("Authorization", authToken).
AddHeader("Authorization", testData.TokenPrefix+authToken).
Expect(t).
Status(200).
JSONSchema(testData.ArticleSingleRespDefinition).
Expand Down
10 changes: 5 additions & 5 deletions implem/gin.server/article_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestRouterHandler_articlePost(t *testing.T) {
t.Run("happycase", func(t *testing.T) {
baloo.New(ts.URL).
Post(artPath).
AddHeader("Authorization", authToken).
AddHeader("Authorization", testData.TokenPrefix+authToken).
BodyString(reqBody).Expect(t).
Status(http.StatusCreated).
JSONSchema(testData.ArticleSingleRespDefinition).
Expand All @@ -69,7 +69,7 @@ func TestRouterHandler_articlePost(t *testing.T) {
t.Run("no body", func(t *testing.T) {
baloo.New(ts.URL).
Post(artPath).
AddHeader("Authorization", authToken).
AddHeader("Authorization", testData.TokenPrefix+authToken).
Expect(t).
Status(http.StatusBadRequest).
Done()
Expand Down Expand Up @@ -111,7 +111,7 @@ func TestRouterHandler_articlePut(t *testing.T) {
t.Run("happycase", func(t *testing.T) {
baloo.New(ts.URL).
Put(artPath+"/"+expectedArticle.Slug).
AddHeader("Authorization", authToken).
AddHeader("Authorization", testData.TokenPrefix+authToken).
BodyString(reqBody).
Expect(t).
Status(http.StatusOK).
Expand All @@ -131,7 +131,7 @@ func TestRouterHandler_articlePut(t *testing.T) {
t.Run("no body", func(t *testing.T) {
baloo.New(ts.URL).
Put(artPath+"/"+expectedArticle.Slug).
AddHeader("Authorization", authToken).
AddHeader("Authorization", testData.TokenPrefix+authToken).
Expect(t).
Status(http.StatusBadRequest).
Done()
Expand Down Expand Up @@ -185,7 +185,7 @@ func TestRouterHandler_articleDelete(t *testing.T) {

baloo.New(ts.URL).
Delete(artPath+"/"+expectedArticle.Slug).
AddHeader("Authorization", authToken).
AddHeader("Authorization", testData.TokenPrefix+authToken).
Expect(t).
Status(http.StatusOK).
Done()
Expand Down
4 changes: 2 additions & 2 deletions implem/gin.server/articles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func TestArticlesFeed(t *testing.T) {

baloo.New(ts.URL).
Get(articlesFeedPath).
AddHeader("Authorization", authToken).
AddHeader("Authorization", testData.TokenPrefix+authToken).
AddQuery("limit", strconv.Itoa(limit)).
AddQuery("offset", strconv.Itoa(offset)).
Expect(t).
Expand Down Expand Up @@ -122,7 +122,7 @@ func TestArticlesFeed(t *testing.T) {

baloo.New(ts.URL).
Get(articlesFeedPath).
AddHeader("Authorization", authToken).
AddHeader("Authorization", testData.TokenPrefix+authToken).
AddQuery("limit", strconv.Itoa(limit)).
AddQuery("offset", strconv.Itoa(offset)).
Expect(t).
Expand Down
2 changes: 1 addition & 1 deletion implem/gin.server/profileFollowDelete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestProfileFollowDelete_happyCase(t *testing.T) {

baloo.New(ts.URL).
Delete(profileFollowDeletePath).
AddHeader("Authorization", authToken).
AddHeader("Authorization", testData.TokenPrefix+authToken).
Expect(t).
Status(200).
JSONSchema(testData.ProfileRespDefinition).
Expand Down
2 changes: 1 addition & 1 deletion implem/gin.server/profileFollowPost_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestProfileFollowPost_happyCase(t *testing.T) {

baloo.New(ts.URL).
Post(profileFollowPostPath).
AddHeader("Authorization", authToken).
AddHeader("Authorization", testData.TokenPrefix+authToken).
Expect(t).
Status(200).
JSONSchema(testData.ProfileRespDefinition).
Expand Down
7 changes: 5 additions & 2 deletions implem/gin.server/profileGet.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ import (
func (rH RouterHandler) profileGet(c *gin.Context) {
log := rH.log(rH.MethodAndPath(c))
requestingUserName := ""
if userName, err := rH.authHandler.GetUserName(c.GetHeader("Authorization")); err == nil {
requestingUserName = userName

if jwt, err := getJWT(c.GetHeader("Authorization")); err == nil {
if userName, err := rH.authHandler.GetUserName(jwt); err == nil {
requestingUserName = userName
}
}

user, follows, err := rH.ucHandler.ProfileGet(requestingUserName, c.Param("username"))
Expand Down
2 changes: 1 addition & 1 deletion implem/gin.server/profileGet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func TestProfileGet_happyCaseAuthenticated(t *testing.T) {

baloo.New(ts.URL).
Get(profileGetPath).
AddHeader("Authorization", authToken).
AddHeader("Authorization", testData.TokenPrefix+authToken).
Expect(t).
Status(200).
JSONSchema(testData.ProfileRespDefinition).
Expand Down
4 changes: 2 additions & 2 deletions implem/gin.server/userGet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"gopkg.in/h2non/baloo.v3"
)

var userGetPath = "/api/users"
var userGetPath = "/api/user"

func TestUserGet_happyCase(t *testing.T) {
mockCtrl := gomock.NewController(t)
Expand All @@ -39,7 +39,7 @@ func TestUserGet_happyCase(t *testing.T) {

baloo.New(ts.URL).
Get(userGetPath).
AddHeader("Authorization", authToken).
AddHeader("Authorization", testData.TokenPrefix+authToken).
Expect(t).
Status(http.StatusOK).
JSONSchema(testData.UserRespDefinition).
Expand Down
4 changes: 2 additions & 2 deletions implem/gin.server/userPatch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"gopkg.in/h2non/baloo.v3"
)

var userPutPath = "/api/users"
var userPutPath = "/api/user"

func TestUserPut_happyCase(t *testing.T) {
mockCtrl := gomock.NewController(t)
Expand All @@ -42,7 +42,7 @@ func TestUserPut_happyCase(t *testing.T) {
assert.NoError(t, err)
baloo.New(ts.URL).
Put(userPutPath).
AddHeader("Authorization", token).
AddHeader("Authorization", testData.TokenPrefix+token).
BodyString(`{
"user": {
"bio": "` + testData.User("rick").Email + `",
Expand Down
33 changes: 2 additions & 31 deletions implem/logrus.logger/logrus.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package logger
import (
"log"

"github.com/err0r500/go-cleanarch-skeleton/domain"
"github.com/err0r500/go-realworld-clean/uc"
"github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -57,37 +56,9 @@ func (l LogrusLogger) Log(args ...interface{}) {
l.Logger.Info(args...)
}

//fixme
func (l LogrusLogger) newLog(err error, usecase interface{}) {
switch v := err.(type) {
case *domain.Message:
f := logrus.Fields{
"type": v.MessageType.String(),
"mess": v.Title,
}

f["env"] = l.env

if v.Additional != "" {
f["additional"] = v.Additional
}

ll := l.Logger.WithFields(f)
switch v.MessageLevel {
case domain.MessDebug:
ll.Debug(usecase)
case domain.MessInfo:
ll.Info(usecase)
case domain.MessWarn:
ll.Warn(usecase)
case domain.MessError:
ll.Error(usecase)
case domain.MessFatal:
ll.Fatal(usecase)
}

default:
l.Logger.WithError(err).Error(usecase)
}
l.Logger.WithError(err).Error(usecase)
}

type SimpleLogger struct{}
Expand Down
8 changes: 7 additions & 1 deletion implem/memory.articleRW/readWriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import (

"errors"

"time"

"log"

"github.com/err0r500/go-realworld-clean/domain"
"github.com/err0r500/go-realworld-clean/uc"
)
Expand All @@ -20,9 +24,11 @@ func New() uc.ArticleRW {
}
func (rw rw) Create(article domain.Article) (*domain.Article, error) {
if _, err := rw.GetBySlug(article.Slug); err == nil {
log.Println(err)
return nil, uc.ErrAlreadyInUse
}

article.CreatedAt = time.Now()
article.UpdatedAt = time.Now()
rw.store.Store(article.Slug, article)

return rw.GetBySlug(article.Slug)
Expand Down
14 changes: 1 addition & 13 deletions testData/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,7 @@ var jane = domain.User{
Password: "janePassword",
}

//func Profile(name string) domain.Profile {
// switch name {
// case "janeFollowingRick":
// return janeFollowingRick
// default:
// return janeFollowingRick
// }
//}

//var janeFollowingRick = domain.Profile{
// User: rick,
// Following: true,
//}
const TokenPrefix = "Token "

var janeArticle = domain.Article{
Slug: "articleSlug",
Expand Down
4 changes: 2 additions & 2 deletions uc/article.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ func (i interactor) ArticlePost(username string, article domain.Article) (*domai
}

slug := i.slugger.NewSlug(article.Title)
if _, err := i.getArticleAndCheckUser("", slug); err != errArticleNotFound {
return nil, errors.New("this title is already taken by another article")
if _, err := i.articleRW.GetBySlug(slug); err == nil {
return nil, ErrAlreadyInUse
}

article.Slug = slug
Expand Down
4 changes: 3 additions & 1 deletion uc/article_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package uc_test
import (
"testing"

"errors"

"github.com/err0r500/go-realworld-clean/domain"
mock "github.com/err0r500/go-realworld-clean/implem/mock.uc"
"github.com/err0r500/go-realworld-clean/testData"
Expand All @@ -23,7 +25,7 @@ func TestInteractor_ArticlePost(t *testing.T) {
i := mock.NewMockedInteractor(mockCtrl)
i.UserRW.EXPECT().GetByName(rick.Name).Return(&rick, nil).Times(1)
i.Slugger.EXPECT().NewSlug(article.Title).Return(slug).Times(1)
i.ArticleRW.EXPECT().GetBySlug(slug).Return(nil, nil).Times(1)
i.ArticleRW.EXPECT().GetBySlug(slug).Return(nil, errors.New("not found")).Times(1)
i.ArticleValidator.EXPECT().BeforeCreationCheck(gomock.Any()).Return(nil).Times(1)
i.ArticleRW.EXPECT().Create(gomock.Any())

Expand Down

0 comments on commit 2a83b05

Please sign in to comment.