Skip to content

Commit

Permalink
10years_token (#316)
Browse files Browse the repository at this point in the history
* update

* update
  • Loading branch information
swuecho authored May 16, 2023
1 parent 795ca48 commit cfef90b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
19 changes: 19 additions & 0 deletions api/chat_auth_user_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func (h *AuthUserHandler) Register(router *mux.Router) {
router.HandleFunc("/users/{id}", h.UpdateSelf).Methods(http.MethodPut)
router.HandleFunc("/signup", h.SignUp).Methods(http.MethodPost)
router.HandleFunc("/login", h.Login).Methods(http.MethodPost)
router.HandleFunc("/token_10years", h.ForeverToken).Methods(http.MethodGet)
// admin
router.HandleFunc("/admin/users", h.CreateUser).Methods(http.MethodPost)
// change user first name, last name
Expand Down Expand Up @@ -188,6 +189,24 @@ func (h *AuthUserHandler) Login(w http.ResponseWriter, r *http.Request) {

}

func (h *AuthUserHandler) ForeverToken(w http.ResponseWriter, r *http.Request) {

lifetime := time.Duration(10*365*24) * time.Hour
userId, _ := getUserID(r.Context())
userRole := r.Context().Value(userContextKey).(string)
token, err := auth.GenerateToken(userId, userRole, jwtSecretAndAud.Secret, jwtSecretAndAud.Audience, lifetime)

if err != nil {
RespondWithError(w, http.StatusInternalServerError, "error.fail_to_generate_token", err)
return
}
w.Header().Set("Content-Type", "application/json")
expiresIn := time.Now().Add(lifetime).Unix()
json.NewEncoder(w).Encode(TokenResult{AccessToken: token, ExpiresIn: int(expiresIn)})
w.WriteHeader(http.StatusOK)

}

func (h *AuthUserHandler) Logout(w http.ResponseWriter, r *http.Request) {
c, err := r.Cookie("jwt")
if err != nil {
Expand Down
2 changes: 0 additions & 2 deletions api/chat_model_privilege_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ func (h *UserChatModelPrivilegeHandler) Register(r *mux.Router) {

}


type ChatModelPrivilege struct {
ID int32 `json:"id"`
FullName string `json:"fullName"`
Expand All @@ -53,7 +52,6 @@ type ChatModelPrivilege struct {
RateLimit int32 `json:"rateLimit"`
}


func (h *UserChatModelPrivilegeHandler) ListUserChatModelPrivileges(w http.ResponseWriter, r *http.Request) {
// TODO: check user is super_user
userChatModelRows, err := h.db.ListUserChatModelPrivilegesRateLimit(r.Context())
Expand Down
2 changes: 0 additions & 2 deletions api/chat_user_active_chat_sesion_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ func (h *UserActiveChatSessionHandler) GetUserActiveChatSessionHandler(w http.Re
json.NewEncoder(w).Encode(session)
}



// CreateOrUpdateUserActiveChatSessionHandler handles POST requests to create a new session.
func (h *UserActiveChatSessionHandler) CreateOrUpdateUserActiveChatSessionHandler(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
Expand Down

0 comments on commit cfef90b

Please sign in to comment.