Refactor login response structure to enhance clarity and security
- Removed the refreshCaptcha function from the login.go file to streamline the codebase. - Updated the authback function in oauth.go to return a structured LoginSuccessResponse, including session ID, access token, refresh token, and their expiration times for improved response clarity. - Introduced a new LoginSuccessResponse type in types.go to standardize the login success response format.
This commit is contained in:
parent
7977b7f48e
commit
637558aeb7
3 changed files with 18 additions and 26 deletions
|
|
@ -78,31 +78,6 @@ func getCaptcha(c *gin.Context) {
|
|||
})
|
||||
}
|
||||
|
||||
// refreshCaptcha is the handler for refresh captcha image
|
||||
func refreshCaptcha(c *gin.Context) {
|
||||
var option helper.CaptchaOption = helper.NewCaptchaOption()
|
||||
|
||||
err := c.ShouldBindQuery(&option)
|
||||
if err != nil {
|
||||
response.RespondWithError(c, http.StatusBadRequest, &response.ErrorResponse{
|
||||
Code: response.ErrInvalidRequest.Code,
|
||||
ErrorDescription: err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// Set the type to image
|
||||
option.Type = "image"
|
||||
id, content := helper.CaptchaMake(option)
|
||||
|
||||
// Return in the format expected by the frontend
|
||||
response.RespondWithSuccess(c, http.StatusOK, gin.H{
|
||||
"captcha_id": id,
|
||||
"captcha_image": content,
|
||||
"expires_in": 300, // 5 minutes
|
||||
})
|
||||
}
|
||||
|
||||
// LoginThirdParty is the handler for third party login
|
||||
func LoginThirdParty(providerID string, userinfo *oauthtypes.OIDCUserInfo, ip string) (*LoginResponse, error) {
|
||||
|
||||
|
|
|
|||
|
|
@ -188,7 +188,14 @@ func authback(c *gin.Context) {
|
|||
SendLoginCookies(c, loginResponse, sid)
|
||||
|
||||
// Send IDToken to the client
|
||||
response.RespondWithSuccess(c, response.StatusOK, map[string]interface{}{"id_token": loginResponse.IDToken})
|
||||
response.RespondWithSuccess(c, response.StatusOK, LoginSuccessResponse{
|
||||
SessionID: sid,
|
||||
IDToken: loginResponse.IDToken,
|
||||
AccessToken: loginResponse.AccessToken,
|
||||
RefreshToken: loginResponse.RefreshToken,
|
||||
ExpiresIn: loginResponse.ExpiresIn,
|
||||
RefreshTokenExpiresIn: loginResponse.RefreshTokenExpiresIn,
|
||||
})
|
||||
}
|
||||
|
||||
// getOAuthAuthorizationURL generates OAuth authorization URL for a provider
|
||||
|
|
|
|||
|
|
@ -172,6 +172,16 @@ type LoginResponse struct {
|
|||
Scope string `json:"scope,omitempty"`
|
||||
}
|
||||
|
||||
// LoginSuccessResponse represents the response for login success
|
||||
type LoginSuccessResponse struct {
|
||||
IDToken string `json:"id_token,omitempty"`
|
||||
AccessToken string `json:"access_token,omitempty"`
|
||||
SessionID string `json:"session_id,omitempty"`
|
||||
RefreshToken string `json:"refresh_token,omitempty"`
|
||||
ExpiresIn int `json:"expires_in,omitempty"`
|
||||
RefreshTokenExpiresIn int `json:"refresh_token_expires_in,omitempty"`
|
||||
}
|
||||
|
||||
// Built-in preset mapping types
|
||||
const (
|
||||
MappingGoogle = "google"
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue