Skip to content

Commit

Permalink
fix(oauth): handle oauth2 error_description
Browse files Browse the repository at this point in the history
  • Loading branch information
Darkness4 committed Jan 13, 2025
1 parent f3d2454 commit db9d8a3
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions auth/oauth/oauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,14 @@ func (a *OAuth) Login() http.HandlerFunc {
}
// State contain the provider and the csrf token.
state := fmt.Sprintf("%s,%s", token, p)
authCodeURL := provider.AuthCodeURL(state)
fmt.Println(authCodeURL)

http.SetCookie(w, cookie)
http.Redirect(
w,
r,
provider.AuthCodeURL(state),
authCodeURL,
http.StatusFound,
)
}
Expand Down Expand Up @@ -82,6 +85,12 @@ func (a *OAuth) CallBack() http.HandlerFunc {
return
}

errorDescription := val.Get("error_description")
if errorDescription != "" {
http.Error(w, errorDescription, http.StatusUnauthorized)
return
}

expectedCSRF, err := r.Cookie("csrf_token")
if err == http.ErrNoCookie {
http.Error(w, "no csrf cookie error", http.StatusUnauthorized)
Expand All @@ -100,19 +109,19 @@ func (a *OAuth) CallBack() http.HandlerFunc {

oauth2Token, err := provider.Exchange(r.Context(), code)
if err != nil {
http.Error(w, err.Error(), http.StatusUnauthorized)
http.Error(w, fmt.Sprintf("exchange: %v", err), http.StatusUnauthorized)
return
}

userID, userName, err := provider.GetIdentity(r.Context(), oauth2Token)
if err != nil {
http.Error(w, err.Error(), http.StatusUnauthorized)
http.Error(w, fmt.Sprintf("id: %v", err), http.StatusUnauthorized)
return
}

token, err := a.JWTSecret.GenerateToken(userID, userName, strings.ToLower(p))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
http.Error(w, fmt.Sprintf("gen token: %v", err), http.StatusInternalServerError)
return
}

Expand Down

0 comments on commit db9d8a3

Please sign in to comment.