Skip to content

Commit

Permalink
Merge pull request #3 from cho4036/user_resource_api
Browse files Browse the repository at this point in the history
DB 연동 로직 추가
  • Loading branch information
ktkfree authored Mar 23, 2023
2 parents 1765846 + 1b3c189 commit 031541a
Show file tree
Hide file tree
Showing 18 changed files with 1,360 additions and 598 deletions.
2 changes: 0 additions & 2 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ func init() {
flag.String("keycloak-address", "https://keycloak-kyuho.taco-cat.xyz/auth", "URL of keycloak")
flag.String("keycloak-admin", "admin", "user of keycloak")
flag.String("keycloak-password", "admin", "password of keycloak")
flag.String("keycloak-realm", "tks", "realm of keycloak")
flag.String("keycloak-client-secret", keycloak.DefaultClientSecret, "realm of keycloak")

pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
Expand Down Expand Up @@ -103,7 +102,6 @@ func main() {
AdminId: viper.GetString("keycloak-admin"),
AdminPassword: viper.GetString("keycloak-password"),
ClientSecret: viper.GetString("keycloak-client-secret"),
MasterRealm: keycloak.DefaultMasterRealm,
})

err = keycloak.InitializeKeycloak()
Expand Down
12 changes: 7 additions & 5 deletions internal/auth/request/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type key int
const (
userKey key = iota
userToken
organinzation
)

func WithValue(parent context.Context, key, val interface{}) context.Context {
Expand All @@ -19,6 +20,12 @@ func WithUser(parent context.Context, user user.Info) context.Context {
return WithValue(parent, userKey, user)
}

// UserFrom function to retrieve user from context. If there is no user in context, it returns false
func UserFrom(ctx context.Context) (user.Info, bool) {
user, ok := ctx.Value(userKey).(user.Info)
return user, ok
}

func WithToken(parent context.Context, token string) context.Context {
return WithValue(parent, userToken, token)
}
Expand All @@ -27,8 +34,3 @@ func TokenFrom(ctx context.Context) (string, bool) {
token, ok := ctx.Value(userToken).(string)
return token, ok
}

func UserFrom(ctx context.Context) (user.Info, bool) {
user, ok := ctx.Value(userKey).(user.Info)
return user, ok
}
113 changes: 13 additions & 100 deletions internal/delivery/http/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,27 @@ package http

import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"

"github.com/openinfradev/tks-api/internal/auth/request"
"github.com/openinfradev/tks-api/internal/usecase"
"github.com/openinfradev/tks-api/pkg/domain"
"github.com/openinfradev/tks-api/pkg/httpErrors"
"github.com/openinfradev/tks-api/pkg/log"
)

type IAuthHandler interface {
Login(w http.ResponseWriter, r *http.Request)
SingUp(w http.ResponseWriter, r *http.Request)
GetRole(w http.ResponseWriter, r *http.Request)
Logout(w http.ResponseWriter, r *http.Request)
FindId(w http.ResponseWriter, r *http.Request)
FindPassword(w http.ResponseWriter, r *http.Request)

//Authenticate(next http.Handler) http.Handler
}
type AuthHandler struct {
usecase usecase.IAuthUsecase
}

func NewAuthHandler(h usecase.IAuthUsecase) *AuthHandler {
func NewAuthHandler(h usecase.IAuthUsecase) IAuthHandler {
return &AuthHandler{
usecase: h,
}
Expand Down Expand Up @@ -81,101 +78,17 @@ func (h *AuthHandler) Login(w http.ResponseWriter, r *http.Request) {
// @Success 200 {object} object
// @Router /auth/logout [post]
func (h *AuthHandler) Logout(w http.ResponseWriter, r *http.Request) {

}

// CreateUser godoc
// @Tags Users
// @Summary CreateUser
// @Description CreateUser
// @Accept json
// @Produce json
// @Param body body domain.CreateUserRequest true "user info"
// @Success 200 {object} domain.User
// @Router /users [post]
// @Security JWT
func (h *AuthHandler) CreateUser(w http.ResponseWriter, r *http.Request) {
input := domain.CreateUserRequest{}
body, err := io.ReadAll(r.Body)
if err != nil {
ErrorJSON(w, httpErrors.NewBadRequestError(err))
return
}

if err = json.Unmarshal(body, &input); err != nil {
ErrorJSON(w, httpErrors.NewBadRequestError(err))
return
}

token, ok := request.TokenFrom(r.Context())
if !ok {
ErrorJSON(w, errors.New("token not found"))
return
}
log.Info("Send signup request to keycloak")
user, err := h.usecase.Register(input.AccountId, input.Password, input.Name, input.OrganizationName, input.Role, token)
if err != nil {
ErrorJSON(w, err)
return
}

var out struct {
User domain.User
}

out.User = user

ResponseJSON(w, http.StatusOK, out)
}

// GetUsers godoc
// @Tags Users
// @Summary GetUsers
// @Description GetUsers
// @Accept json
// @Produce json
// @Param body body domain.CreateUserRequest true "user info"
// @Success 200 {object} domain.User
// @Router /users [post]
// @Security JWT
func (h *AuthHandler) GetUsers(w http.ResponseWriter, r *http.Request) {
ErrorJSON(w, fmt.Errorf("Need implementation"))
// Do nothing
// Token is not able to be expired manually. Therefore, nothing to do currently.
ResponseJSON(w, http.StatusOK, nil)
}

// GetUser godoc
// @Tags Users
// @Summary GetUser
// @Description GetUser
// @Accept json
// @Produce json
// @Param userId path string true "user uid"
// @Success 200 {object} domain.User
// @Router /users/:userId [get]
// @Security JWT
func (h *AuthHandler) GetUser(w http.ResponseWriter, r *http.Request) {
ErrorJSON(w, fmt.Errorf("Need implementation"))
func (h *AuthHandler) FindId(w http.ResponseWriter, r *http.Request) {
//TODO implement me
panic("implement me")
}

// GetRoles godoc
// @Tags Auth
// @Summary roles
// @Description roles
// @Accept json
// @Produce json
// @Success 200 {object} []domain.Role
// @Router /auth/roles [get]
// @Security JWT
func (h *AuthHandler) GetRoles(w http.ResponseWriter, r *http.Request) {
roles, err := h.usecase.FetchRoles()
if err != nil {
ErrorJSON(w, err)
return
}

var out struct {
Roles []domain.Role
}
out.Roles = roles

ResponseJSON(w, http.StatusOK, out)
func (h *AuthHandler) FindPassword(w http.ResponseWriter, r *http.Request) {
//TODO implement me
panic("implement me")
}
43 changes: 31 additions & 12 deletions internal/delivery/http/organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,28 @@ package http
import (
"encoding/json"
"fmt"
"github.com/go-playground/validator"
"github.com/openinfradev/tks-api/pkg/httpErrors"
"io"
"net/http"
"time"

"github.com/go-playground/validator"
"github.com/gorilla/mux"
"github.com/openinfradev/tks-api/internal/auth/request"
"github.com/openinfradev/tks-api/internal/usecase"
"github.com/openinfradev/tks-api/pkg/domain"
"github.com/openinfradev/tks-api/pkg/httpErrors"
"github.com/openinfradev/tks-api/pkg/log"
)

type OrganizationHandler struct {
usecase usecase.IOrganizationUsecase
organizationUsecase usecase.IOrganizationUsecase
userUsecase usecase.IUserUsecase
}

func NewOrganizationHandler(h usecase.IOrganizationUsecase) *OrganizationHandler {
func NewOrganizationHandler(o usecase.IOrganizationUsecase, u usecase.IUserUsecase) *OrganizationHandler {
return &OrganizationHandler{
usecase: h,
organizationUsecase: o,
userUsecase: u,
}
}

Expand Down Expand Up @@ -65,7 +67,7 @@ func (h *OrganizationHandler) CreateOrganization(w http.ResponseWriter, r *http.
return
}

organizationId, err := h.usecase.Create(domain.Organization{
organizationId, err := h.organizationUsecase.Create(domain.Organization{
Name: input.Name,
Creator: userId.String(),
Description: input.Description,
Expand All @@ -77,6 +79,14 @@ func (h *OrganizationHandler) CreateOrganization(w http.ResponseWriter, r *http.
return
}

// Admin user 생성
_, err = h.userUsecase.CreateAdmin(organizationId)
if err != nil {
log.Error("Failed to create user err : ", err)
ErrorJSON(w, err)
return
}

// add user to organizationUser
/*
err = h.Repository.AddUserInOrganization(userId, response.GetOrganizationId())
Expand Down Expand Up @@ -110,7 +120,8 @@ func (h *OrganizationHandler) CreateOrganization(w http.ResponseWriter, r *http.
// @Router /organizations [get]
// @Security JWT
func (h *OrganizationHandler) GetOrganizations(w http.ResponseWriter, r *http.Request) {
organizations, err := h.usecase.Fetch()
log.Info("GetOrganization")
organizations, err := h.organizationUsecase.Fetch()
if err != nil {
log.Error("Failed to get organizations err : ", err)
ErrorJSON(w, err)
Expand Down Expand Up @@ -144,7 +155,7 @@ func (h *OrganizationHandler) GetOrganization(w http.ResponseWriter, r *http.Req
return
}

organization, err := h.usecase.Get(organizationId)
organization, err := h.organizationUsecase.Get(organizationId)
if err != nil {
ErrorJSON(w, err)
return
Expand Down Expand Up @@ -183,7 +194,15 @@ func (h *OrganizationHandler) DeleteOrganization(w http.ResponseWriter, r *http.
return
}

err := h.usecase.Delete(organizationId, token)
// TODO : organization에 속한 user들도 삭제해야함(DB에는 남아있음)
// Admin user 삭제
err := h.userUsecase.DeleteAdmin(organizationId)
ResponseJSON(w, http.StatusOK, nil)

// TODO: user 삭제

// organization 삭제
err = h.organizationUsecase.Delete(organizationId, token)
if err != nil {
ErrorJSON(w, err)
return
Expand All @@ -192,7 +211,7 @@ func (h *OrganizationHandler) DeleteOrganization(w http.ResponseWriter, r *http.
ResponseJSON(w, http.StatusOK, nil)
}

// GetOrganization godoc
// UpdateOrganization godoc
// @Tags Organizations
// @Summary Update organization detail
// @Description Update organization detail
Expand All @@ -206,7 +225,7 @@ func (h *OrganizationHandler) UpdateOrganization(w http.ResponseWriter, r *http.
vars := mux.Vars(r)
organizationId, ok := vars["organizationId"]
if !ok {
ErrorJSON(w, httpErrors.NewBadRequestError(fmt.Errorf("Invalid organizationId")))
ErrorJSON(w, httpErrors.NewBadRequestError(fmt.Errorf("invalid organizationId")))
return
}

Expand All @@ -223,7 +242,7 @@ func (h *OrganizationHandler) UpdateOrganization(w http.ResponseWriter, r *http.
return
}

err = h.usecase.Update(organizationId, input)
err = h.organizationUsecase.Update(organizationId, input)
if err != nil {
ErrorJSON(w, err)
return
Expand Down
Loading

0 comments on commit 031541a

Please sign in to comment.