From 1570ea51864c06dbb47b9e2fd78ddb539983434b Mon Sep 17 00:00:00 2001 From: Kyungeun Park <63892989+parkgang@users.noreply.github.com> Date: Sat, 20 Nov 2021 06:26:36 +0900 Subject: [PATCH] =?UTF-8?q?=EC=9D=B4=EB=A9=94=EC=9D=BC=EB=A1=9C=EB=8F=84?= =?UTF-8?q?=20=EC=82=AC=EC=9A=A9=EC=9E=90=20=EC=A1=B0=ED=9A=8C=ED=95=A0=20?= =?UTF-8?q?=EC=88=98=20=EC=9E=88=EB=8F=84=EB=A1=9D=20API=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80=20(#18)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * add: 서비스 데이터는 public이므로 인증 없이 사용자 조회하도록 반영 * rename: 함수명 변경 * up: token 기반으로 사용자 조회하는 api 복구 * rename: 명확하게 이름 변경 * up: 명확하게 변수명 변경 * up: 중복 코드 함수화 * up: 문서화 * up: 누락된 http status 추가 --- docs/docs.go | 46 +++++++++++++++++++++++++- docs/swagger.json | 46 +++++++++++++++++++++++++- docs/swagger.yaml | 31 +++++++++++++++++- internal/app/handlers/users.go | 56 ++++++++++++++++++++------------ internal/app/models/user-info.go | 30 +++++++++++++++++ internal/app/routers/router.go | 3 +- internal/pkg/project/project.go | 2 +- 7 files changed, 188 insertions(+), 26 deletions(-) diff --git a/docs/docs.go b/docs/docs.go index f40cb5c..0a244be 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -122,7 +122,7 @@ var doc = `{ "tags": [ "User" ], - "summary": "사용자 정보 조회", + "summary": "Token으로 사용자 정보 조회", "parameters": [ { "type": "string", @@ -450,6 +450,50 @@ var doc = `{ } } }, + "/users/{userEmail}": { + "get": { + "description": "사용자 이메일으로 사용자를 조회합니다.", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "User" + ], + "summary": "Email로 사용자 정보 조회", + "parameters": [ + { + "type": "string", + "description": "사용자 Email", + "name": "userEmail", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/models.UserInfo" + } + }, + "400": { + "description": "" + }, + "404": { + "description": "" + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/models.ErrResponse" + } + } + } + } + }, "/version": { "get": { "description": "앱 버전을 응답합니다.", diff --git a/docs/swagger.json b/docs/swagger.json index c1aa19c..4b9ba62 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -102,7 +102,7 @@ "tags": [ "User" ], - "summary": "사용자 정보 조회", + "summary": "Token으로 사용자 정보 조회", "parameters": [ { "type": "string", @@ -430,6 +430,50 @@ } } }, + "/users/{userEmail}": { + "get": { + "description": "사용자 이메일으로 사용자를 조회합니다.", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "User" + ], + "summary": "Email로 사용자 정보 조회", + "parameters": [ + { + "type": "string", + "description": "사용자 Email", + "name": "userEmail", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/models.UserInfo" + } + }, + "400": { + "description": "" + }, + "404": { + "description": "" + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/models.ErrResponse" + } + } + } + } + }, "/version": { "get": { "description": "앱 버전을 응답합니다.", diff --git a/docs/swagger.yaml b/docs/swagger.yaml index abebd9d..3e88f4a 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -217,7 +217,36 @@ paths: description: Internal Server Error schema: $ref: '#/definitions/models.ErrResponse' - summary: 사용자 정보 조회 + summary: Token으로 사용자 정보 조회 + tags: + - User + /users/{userEmail}: + get: + consumes: + - application/json + description: 사용자 이메일으로 사용자를 조회합니다. + parameters: + - description: 사용자 Email + in: path + name: userEmail + required: true + type: string + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/models.UserInfo' + "400": + description: "" + "404": + description: "" + "500": + description: Internal Server Error + schema: + $ref: '#/definitions/models.ErrResponse' + summary: Email로 사용자 정보 조회 tags: - User /users/login: diff --git a/internal/app/handlers/users.go b/internal/app/handlers/users.go index 4df8a8c..33ec078 100644 --- a/internal/app/handlers/users.go +++ b/internal/app/handlers/users.go @@ -1,7 +1,6 @@ package handlers import ( - "encoding/base64" "errors" "fmt" "io/ioutil" @@ -252,7 +251,7 @@ func UserKakaoLoginCallBack(c *gin.Context) { c.Redirect(http.StatusFound, redUrl) } -// @Summary 사용자 정보 조회 +// @Summary Token으로 사용자 정보 조회 // @Description token 클레임에 있는 id 값으로 사용자를 조회합니다. // @Tags User // @Accept json @@ -263,7 +262,7 @@ func UserKakaoLoginCallBack(c *gin.Context) { // @Failure 404 // @Failure 500 {object} models.ErrResponse // @Router /users [get] -func UserInfo(c *gin.Context) { +func UserInfoTokenQuey(c *gin.Context) { au, err := auth.ExtractTokenMetadata(c.Request) if err != nil { c.JSON(http.StatusUnauthorized, models.ErrResponse{ @@ -272,10 +271,10 @@ func UserInfo(c *gin.Context) { return } - users := entitys.User{ + user := entitys.User{ Id: uint(au.UserId), } - if err := orm.Client.First(&users).Error; err != nil { + if err := orm.Client.First(&user).Error; err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { c.Status(http.StatusNotFound) return @@ -287,26 +286,41 @@ func UserInfo(c *gin.Context) { return } - // [base64 encoding for any image](https://freshman.tech/snippets/go/image-to-base64/) - var base64Encoding string + res := models.NewUserInfo(user) + c.JSON(http.StatusOK, res) +} - // 이미지 파일의 콘텐츠 유형에 맞게 적절한 URI 체계 헤더를 추가합니다. - mimeType := http.DetectContentType(users.AvatarImage) - switch mimeType { - case "image/jpeg": - base64Encoding += "data:image/jpeg;base64," - case "image/png": - base64Encoding += "data:image/png;base64," +// @Summary Email로 사용자 정보 조회 +// @Description 사용자 이메일으로 사용자를 조회합니다. +// @Tags User +// @Accept json +// @Produce json +// @Param userEmail path string true "사용자 Email" +// @Success 200 {object} models.UserInfo +// @Failure 400 +// @Failure 404 +// @Failure 500 {object} models.ErrResponse +// @Router /users/{userEmail} [get] +func UserInfoEmailQuey(c *gin.Context) { + userEmail := c.Param("userEmail") + if userEmail == "" { + c.Status(http.StatusBadRequest) + return } - base64Encoding += base64.StdEncoding.EncodeToString(users.AvatarImage) - - res := models.UserInfo{ - Id: int(users.Id), - Email: users.Email, - Name: users.Name, - AvatarImage: base64Encoding, + user := entitys.User{} + if err := orm.Client.Where("email = ?", userEmail).Find(&user).Error; err != nil { + c.JSON(http.StatusInternalServerError, models.ErrResponse{ + Message: err.Error(), + }) + return } + if userEmail != user.Email { + c.Status(http.StatusNotFound) + return + } + + res := models.NewUserInfo(user) c.JSON(http.StatusOK, res) } diff --git a/internal/app/models/user-info.go b/internal/app/models/user-info.go index 7a5c104..ef789a5 100644 --- a/internal/app/models/user-info.go +++ b/internal/app/models/user-info.go @@ -1,8 +1,38 @@ package models +import ( + "encoding/base64" + "net/http" + + "github.com/belf-kr/oauth-server/internal/app/entitys" +) + type UserInfo struct { Id int `json:"id" binding:"required" example:"1"` Email string `json:"email" binding:"required" example:"user01@test.com"` Name string `json:"name" binding:"required" example:"사용자01"` AvatarImage string `json:"avatarImage" binding:"required" example:"base64으로 인코딩된 이미지"` } + +func NewUserInfo(user entitys.User) UserInfo { + // [base64 encoding for any image](https://freshman.tech/snippets/go/image-to-base64/) + var base64Encoding string + + // 이미지 파일의 콘텐츠 유형에 맞게 적절한 URI 체계 헤더를 추가합니다. + mimeType := http.DetectContentType(user.AvatarImage) + switch mimeType { + case "image/jpeg": + base64Encoding += "data:image/jpeg;base64," + case "image/png": + base64Encoding += "data:image/png;base64," + } + + base64Encoding += base64.StdEncoding.EncodeToString(user.AvatarImage) + + return UserInfo{ + Id: int(user.Id), + Email: user.Email, + Name: user.Name, + AvatarImage: base64Encoding, + } +} diff --git a/internal/app/routers/router.go b/internal/app/routers/router.go index 83d560a..bd22804 100644 --- a/internal/app/routers/router.go +++ b/internal/app/routers/router.go @@ -16,7 +16,8 @@ func Use(api *gin.RouterGroup) { users.POST("/signup", handlers.UserSignup) users.POST("/login", handlers.UserLogin) users.GET("/login/kakao", handlers.UserKakaoLoginCallBack) - users.GET("", middlewares.TokenAuthMiddleware(), handlers.UserInfo) + users.GET("", middlewares.TokenAuthMiddleware(), handlers.UserInfoTokenQuey) + users.GET(":userEmail", handlers.UserInfoEmailQuey) users.POST("/logout", middlewares.TokenAuthMiddleware(), handlers.UserLogout) users.GET("/token/valid", middlewares.TokenAuthMiddleware(), handlers.UserTokenValid) users.POST("/token/refresh", handlers.UserTokenRefresh) diff --git a/internal/pkg/project/project.go b/internal/pkg/project/project.go index 370a1d5..11efef9 100644 --- a/internal/pkg/project/project.go +++ b/internal/pkg/project/project.go @@ -2,5 +2,5 @@ package project const ( AppName string = "oauth-server" - AppVersion string = "0.2.2" + AppVersion string = "0.3.0" )