Skip to content

Commit

Permalink
create client authentication & fix oauth client store.
Browse files Browse the repository at this point in the history
client store need to test.
  • Loading branch information
Xunop committed Oct 21, 2023
1 parent 6fa63a8 commit d114abe
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
15 changes: 12 additions & 3 deletions api/v1/oauth_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ import (

var (
srv *server.Server
pgxConn, _ = pgx.Connect(context.TODO(), config.Config.Sub("oauth.server").GetString("db_uri"))
pgxConn, _ = pgx.Connect(context.Background(), config.Config.Sub("oauth.server").GetString("db_uri"))
adapter = pgx4adapter.NewConn(pgxConn)
clientStore, _ = pg.NewClientStore(adapter)
)

func init() {
Expand All @@ -41,6 +40,7 @@ func InitServer() {
// use PostgreSQL token store with pgx.Connection adapter
tokenStore, _ := pg.NewTokenStore(adapter, pg.WithTokenStoreGCInterval(time.Minute))
defer tokenStore.Close()
clientStore, _ := pg.NewClientStore(adapter)

mg := manage.NewDefaultManager()
mg.MapTokenStorage(tokenStore)
Expand All @@ -66,7 +66,6 @@ func InitServer() {
srv.SetResponseErrorHandler(func(re *errors.Response) {
log.Println("Response Error:", re.Error.Error())
})

}

// Create client
Expand All @@ -77,18 +76,28 @@ func CreateClient(c *gin.Context) {
return
}

token := c.GetHeader("TOKEN")
uid, err := util.GetUsername(token, model.LOGIN_TOKEN_SUB)
if err != nil || uid == "" {
c.JSON(http.StatusOK, result.Failed(result.TokenError))
return
}

clientID := util.GenerateUUID()
secret, err := util.GenerateRandomString(32)
if err != nil {
c.JSON(http.StatusInternalServerError, result.Failed(result.InternalErr))
return
}

clientStore, _ := pg.NewClientStore(adapter)
cErr := clientStore.Create(&models.Client{
ID: clientID,
Secret: secret,
Domain: redirectURI,
UserID: uid,
})

if cErr != nil {
c.JSON(http.StatusBadRequest, result.Failed(result.InternalErr))
return
Expand Down
2 changes: 1 addition & 1 deletion api/v1/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ func ChangePassword(ctx *gin.Context) {
token := ctx.GetHeader("TOKEN")
uid, err := util.GetUsername(token, model.LOGIN_TOKEN_SUB)
if err != nil || uid == "" {
ctx.JSON(http.StatusOK, result.Failed(result.TicketNotCorrect))
ctx.JSON(http.StatusOK, result.Failed(result.TokenError))
return
}
// Get password from form
Expand Down
21 changes: 11 additions & 10 deletions example/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ func main() {
http.HandleFunc("/api/auth/callback/sastlink", func(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
println(r.URL.RawQuery)
state := r.Form.Get("state")
if state != "xyz" {
http.Error(w, "State invalid", http.StatusBadRequest)
return
}
// state := r.Form.Get("state")
// if state != "xyz" {
// http.Error(w, "State invalid", http.StatusBadRequest)
// return
// }
code := r.Form.Get("code")
if code == "" {
http.Error(w, "Code not found", http.StatusBadRequest)
Expand All @@ -61,11 +61,12 @@ func main() {
http.HandleFunc("/oauth2", func(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
println(r.URL.RawQuery)
state := r.Form.Get("state")
if state != "xyz" {
http.Error(w, "State invalid", http.StatusBadRequest)
return
}
verifier := oauth2.GenerateVerifier()
// state := r.Form.Get("state")
// if state != "xyz" {
// http.Error(w, "State invalid", http.StatusBadRequest)
// return
// }
code := r.Form.Get("code")
if code == "" {
http.Error(w, "Code not found", http.StatusBadRequest)
Expand Down

0 comments on commit d114abe

Please sign in to comment.