Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions auth/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,4 @@ func TestLogin_Success(t *testing.T) {
require.NoError(t, err, "Login should not return an error")
require.NotEmpty(t, token.Token, "Token should not be empty")
require.NotNil(t, token.TokenExpiration, "Token expiration should not be nil")
// profile
userInfo := &UserInfo{Token: token.Token}
profile, err := userInfo.UserProfile(ctx, mockRepo, mockConfig)
require.NoError(t, err)
require.NotEmpty(t, profile)
}
46 changes: 0 additions & 46 deletions auth/basic_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ import (

"github.com/jiaozifs/jiaozifs/config"

"github.com/golang-jwt/jwt"
openapitypes "github.com/oapi-codegen/runtime/types"

"github.com/go-openapi/swag"
logging "github.com/ipfs/go-log/v2"
"github.com/jiaozifs/jiaozifs/api"
Expand Down Expand Up @@ -101,46 +98,3 @@ func (r *Register) Register(ctx context.Context, repo models.IUserRepo) error {
log.Infof("%s registration success", insertUser.Name)
return nil
}

type UserInfo struct {
Token string `json:"token"`
}

func (u *UserInfo) UserProfile(ctx context.Context, repo models.IUserRepo, config *config.AuthConfig) (api.UserInfo, error) {
userInfo := api.UserInfo{}
// Parse JWT Token
token, err := jwt.Parse(u.Token, func(token *jwt.Token) (interface{}, error) {
return config.SecretKey, nil
})
if err != nil {
return userInfo, fmt.Errorf("cannot parse token %s %w", token.Raw, err)
}
// check token validity
if !token.Valid {
return userInfo, fmt.Errorf("token %s invalid %w", token.Raw, ErrInvalidToken)
}
// Get username by token
claims, ok := token.Claims.(jwt.MapClaims)
if !ok {
return userInfo, ErrExtractClaims
}
username := claims["sub"].(string)

// Get user by username
user, err := repo.Get(ctx, models.NewGetUserParams().SetName(username))
if err != nil {
return userInfo, err
}
userInfo = api.UserInfo{
CreatedAt: &user.CreatedAt,
CurrentSignInAt: &user.CurrentSignInAt,
CurrentSignInIP: &user.CurrentSignInIP,
Email: openapitypes.Email(user.Email),
LastSignInAt: &user.LastSignInAt,
LastSignInIP: &user.LastSignInIP,
UpdateAt: &user.UpdatedAt,
Username: user.Name,
}
log.Info("get user profile success")
return userInfo, nil
}
24 changes: 13 additions & 11 deletions controller/user_ctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package controller

import (
"context"
"fmt"
"net/http"

openapitypes "github.com/oapi-codegen/runtime/types"

logging "github.com/ipfs/go-log/v2"

"github.com/gorilla/sessions"
Expand Down Expand Up @@ -71,25 +72,26 @@ func (userCtl UserController) Register(ctx context.Context, w *api.JiaozifsRespo
w.OK()
}

func (userCtl UserController) GetUserInfo(ctx context.Context, w *api.JiaozifsResponse, r *http.Request) {
func (userCtl UserController) GetUserInfo(ctx context.Context, w *api.JiaozifsResponse, _ *http.Request) {
// Get token from Header
user, err := auth.GetUser(ctx)
if err != nil {
w.Error(err)
return
}
fmt.Println(user)
tokenString := r.Header.Get(AuthHeader)
userInfo := &auth.UserInfo{Token: tokenString}

// perform GetUserInfo
usrInfo, err := userInfo.UserProfile(ctx, userCtl.Repo.UserRepo(), userCtl.Config)
if err != nil {
w.Error(err)
return
userInfo := api.UserInfo{
CreatedAt: &user.CreatedAt,
CurrentSignInAt: &user.CurrentSignInAt,
CurrentSignInIP: &user.CurrentSignInIP,
Email: openapitypes.Email(user.Email),
LastSignInAt: &user.LastSignInAt,
LastSignInIP: &user.LastSignInIP,
UpdateAt: &user.UpdatedAt,
Username: user.Name,
}

w.JSON(usrInfo)
w.JSON(userInfo)
}

func (userCtl UserController) Logout(_ context.Context, w *api.JiaozifsResponse, r *http.Request) {
Expand Down