Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix token error when last request error. #60

Merged
merged 2 commits into from
Nov 2, 2023
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
14 changes: 9 additions & 5 deletions api/v1/oauth_client_github.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/NJUPT-SAST/sast-link-backend/config"
"github.com/NJUPT-SAST/sast-link-backend/endpoints"
"github.com/NJUPT-SAST/sast-link-backend/log"
"github.com/NJUPT-SAST/sast-link-backend/model/result"
"github.com/NJUPT-SAST/sast-link-backend/service"
"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -36,9 +37,9 @@ func OauthGithubLogin(c *gin.Context) {
oauthState := GenerateStateOauthCookie(c.Writer)
url := githubConf.AuthCodeURL(oauthState)

fmt.Println("------")
fmt.Printf("Visit the URL for the auth dialog: %v\n", url)
fmt.Println("------")
log.Log.Println("------")
log.Log.Printf("Visit the URL for the auth dialog: %v\n", url)
log.Log.Println("------")

c.Redirect(http.StatusFound, url)
}
Expand All @@ -47,7 +48,7 @@ func OauthGithubCallback(c *gin.Context) {
oauthState, _ := c.Request.Cookie("oauthstate")

if c.Request.FormValue("state") != oauthState.Value {
fmt.Printf("invalid oauth state, expected '%s', got '%s'\n", oauthState.Value, c.Request.FormValue("state"))
log.Log.Printf("invalid oauth state, expected '%s', got '%s'\n", oauthState.Value, c.Request.FormValue("state"))
c.Redirect(http.StatusFound, "/")
return
}
Expand Down Expand Up @@ -83,24 +84,27 @@ func getUserInfoFromGithub(ctx context.Context, code string) (string, error) {

token, err := githubConf.Exchange(ctx, code)
if err != nil {
log.Log.Errorf("exchange github code error: %s", err.Error())
return "", fmt.Errorf("exchange github code error: %s", err.Error())
}
client := &http.Client{}
req, err := http.NewRequest("GET", GithubUserInfoURL, nil)
if err != nil {
log.Log.Errorf("new request error: %s", err.Error())
return "", fmt.Errorf("new request error: %s", err.Error())
}
req.Header.Set("Authorization", "Bearer "+token.AccessToken)
res, err := client.Do(req)
if err != nil {
log.Log.Errorf("failt to getting user info: %s", err.Error())
return "", fmt.Errorf("failt to getting user info: %s", err.Error())
}
body, err := io.ReadAll(res.Body)
if err != nil {
return "", result.InternalErr
}

// Now just get the github id
// TODO:Now just get the github id
githubId := gjson.Get(string(body), "id").String()
return githubId, nil
}
91 changes: 49 additions & 42 deletions api/v1/oauth_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"net/url"
"strings"
"time"

"github.com/NJUPT-SAST/sast-link-backend/config"
"github.com/NJUPT-SAST/sast-link-backend/log"
reqLog "github.com/NJUPT-SAST/sast-link-backend/log"
"github.com/NJUPT-SAST/sast-link-backend/model"
"github.com/NJUPT-SAST/sast-link-backend/model/result"
Expand All @@ -21,7 +20,6 @@ import (
"github.com/go-oauth2/oauth2/v4/manage"
"github.com/go-oauth2/oauth2/v4/models"
"github.com/go-oauth2/oauth2/v4/server"
"github.com/go-session/session"
"github.com/jackc/pgx/v4"
"github.com/sirupsen/logrus"
pg "github.com/vgarvardt/go-oauth2-pg/v4"
Expand Down Expand Up @@ -68,7 +66,7 @@ func InitServer() {
}

func InternalErrorHandler(err error) (re *errors.Response) {
log.Printf("Oauth2:InternalErrorHandler:[%s]", err.Error())
log.Log.Errorf("Oauth2 ::: InternalErrorHandler:[%s]", err.Error())
error := errors.NewResponse(err, http.StatusInternalServerError)
error.ErrorCode = 500
error.StatusCode = http.StatusInternalServerError
Expand All @@ -77,7 +75,7 @@ func InternalErrorHandler(err error) (re *errors.Response) {
}

func ResponseErrorHandler(re *errors.Response) {
log.Printf("Oauth2:ResponseErrorHandler:[%s]", re.Error.Error())
log.Log.Errorf("Oauth2 ::: ResponseErrorHandler:[%s]", re.Error.Error())
}

func ResponseTokenHandler(w http.ResponseWriter, data map[string]interface{}, header http.Header, statusCode ...int) error {
Expand All @@ -94,9 +92,9 @@ func ResponseTokenHandler(w http.ResponseWriter, data map[string]interface{}, he
status = statusCode[0]
}

log.Printf("Oauth2:ResponseTokenHandler:data:[%s]", data)
log.Printf("Oauth2:ResponseTokenHandler:status:[%d]", status)
log.Printf("Oauth2:ResponseTokenHandler:header:[%s]", header)
log.Log.Errorf("Oauth2 ::: ResponseTokenHandler:data:[%s]", data)
log.Log.Errorf("Oauth2 ::: ResponseTokenHandler:status:[%d]", status)
log.Log.Errorf("Oauth2 ::: ResponseTokenHandler:header:[%s]", header)

w.WriteHeader(status)
if data["error"] != nil {
Expand All @@ -109,7 +107,7 @@ func ResponseTokenHandler(w http.ResponseWriter, data map[string]interface{}, he
errCode = data["error_code"].(int)
}
error := result.LocalError{errCode, errMsg, nil}
log.Printf("Oauth2:ResponseTokenHandler:error:[%s]", error)
log.Log.Errorf("Oauth2 ::: ResponseTokenHandler:error:[%s]", error)
return json.NewEncoder(w).Encode(result.Failed(error))
} else {
return json.NewEncoder(w).Encode(result.Success(data))
Expand Down Expand Up @@ -215,27 +213,26 @@ func OauthUserInfo(c *gin.Context) {
func Authorize(c *gin.Context) {
r := c.Request
w := c.Writer
store, err := session.Start(c, w, r)
if err != nil {
c.JSON(http.StatusInternalServerError, result.Failed(result.InternalErr.Wrap(err)))
return
}
// store, err := session.Start(c, w, r)
// if err != nil {
// c.JSON(http.StatusInternalServerError, result.Failed(result.InternalErr.Wrap(err)))
// return
// }
_ = r.ParseForm()
var form url.Values
if v, ok := store.Get("ReturnUri"); ok {
form = v.(url.Values)
}
r.Form = form

store.Delete("ReturnUri")
_ = store.Save()
// var form url.Values
// if v, ok := store.Get("ReturnUri"); ok {
// form = v.(url.Values)
// }
// r.Form = form
// store.Delete("ReturnUri")
// _ = store.Save()

// Redirect user to login page if user not login or
// Get code directly if user has logged in
reqLog.LogReq(r)
err = srv.HandleAuthorizeRequest(w, r)
err := srv.HandleAuthorizeRequest(w, r)
clients, _ := srv.Manager.GetClient(r.Context(), r.Form.Get("client_id"))
log.Println(clients)
log.Log.Println(clients)
if err != nil {
c.JSON(http.StatusInternalServerError, result.Failed(result.InternalErr.Wrap(err)))
return
Expand Down Expand Up @@ -266,8 +263,10 @@ func AccessToken(c *gin.Context) {
err := srv.HandleTokenRequest(w, r)
id, _, _ := clientInfoHandler(r)
var item ClientStoreItem
// TODO: DEBUG
if err := clientAdapter.SelectOne(c, &item, fmt.Sprintf("SELECT * FROM %s WHERE id = $1", "oauth2_clients"), id); err != nil {
log.Printf("----DEBUG----: %s", err.Error())
log.Log.Errorf("----DEBUG----: %s", err.Error())
log.Log.Printf("\nitem: %v\n", item)
return
}

Expand Down Expand Up @@ -323,12 +322,20 @@ func clientInfoHandler(r *http.Request) (clientID, clientSecret string, err erro

}

func userAuthorizeHandler(w http.ResponseWriter, r *http.Request) (userID string, err error) {
session, err := session.Start(r.Context(), w, r)
//session := sessions.Default(c)
func getTokenByUUID(c context.Context, uuid string) (token string, err error) {
token, err = model.Rdb.Get(c, uuid).Result()
if err != nil {
return
log.Log.Errorln("invalid uuid, can't find token")
return "", err
}
return token, nil
}

func userAuthorizeHandler(w http.ResponseWriter, r *http.Request) (userID string, err error) {
// session, err := session.Start(r.Context(), w, r)
// if err != nil {
// return
// }

token := r.Form.Get("part")
if token == "" {
Expand All @@ -338,30 +345,30 @@ func userAuthorizeHandler(w http.ResponseWriter, r *http.Request) (userID string
_ = r.ParseForm()
}

session.Set("ReturnUri", r.Form)
_ = session.Save()
// session.Set("ReturnUri", r.Form)
// _ = session.Save()

w.Header().Set("Content-Type", "application/json")
response := result.Failed(result.TokenError)
log.Println("Oauth2: token is empty")
log.Log.Errorln("Oauth2 ::: token is empty")
json, _ := json.Marshal(response)
w.Write(json)
return
}

username, err := util.GetUsername(token, model.LOGIN_TOKEN_SUB)
log.Println("Oauth2:username: ", username)
log.Log.Println("Oauth2 ::: username: ", username)
if err != nil || username == "" {
if r.Form == nil {
_ = r.ParseForm()
}

session.Set("ReturnUri", r.Form)
_ = session.Save()
// session.Set("ReturnUri", r.Form)
// _ = session.Save()

w.Header().Set("Content-Type", "application/json")
response := result.Failed(result.TokenError)
log.Println("Oauth2: token is invalid")
log.Log.Errorln("Oauth2 ::: token is invalid")
json, _ := json.Marshal(response)
w.Write(json)
return
Expand All @@ -373,12 +380,12 @@ func userAuthorizeHandler(w http.ResponseWriter, r *http.Request) (userID string
_ = r.ParseForm()
}

session.Set("ReturnUri", r.Form)
_ = session.Save()
// session.Set("ReturnUri", r.Form)
// _ = session.Save()

w.Header().Set("Content-Type", "application/json")
response := result.Failed(result.TokenError)
log.Println("Oauth2: token is invalid")
log.Log.Errorln("Oauth2 ::: token is invalid")
json, _ := json.Marshal(response)
w.Write(json)
return
Expand All @@ -388,12 +395,12 @@ func userAuthorizeHandler(w http.ResponseWriter, r *http.Request) (userID string
_ = r.ParseForm()
}

session.Set("ReturnUri", r.Form)
_ = session.Save()
// session.Set("ReturnUri", r.Form)
// _ = session.Save()

w.Header().Set("Content-Type", "application/json")
response := result.Failed(result.TokenError)
log.Println("Oauth2: token is invalid")
log.Log.Errorln("Oauth2 ::: token is invalid")
json, _ := json.Marshal(response)
w.Write(json)
return
Expand Down
2 changes: 1 addition & 1 deletion model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type PostgresConf struct {
func init() {
connectPostgreSQL()
connectRedis()
newStore()
// newStore()
}

// Get redis config
Expand Down
10 changes: 9 additions & 1 deletion model/result/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,18 @@ func Success(data any) Response {
}

func Failed(e LocalError) Response {
if e.Err == nil {
return Response{
Success: false,
ErrCode: e.ErrCode,
ErrMsg: e.ErrMsg,
Data: nil,
}
}
return Response{
Success: false,
ErrCode: e.ErrCode,
ErrMsg: e.ErrMsg,
Data: nil,
Data: e.Error(),
}
}