Skip to content

Commit

Permalink
修复Oauth2插件bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Dot-Liu committed Jan 22, 2024
1 parent 5c47b4b commit aacd9c0
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 17 deletions.
2 changes: 1 addition & 1 deletion application/auth/oauth2/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (c *client) MatchSecret(clientSecret string) error {
secret := pbkdf2.Key([]byte(clientSecret), salt, c.hashRule.iterations, c.hashRule.length, sha512.New)
clientSecret = base64.RawStdEncoding.EncodeToString(secret)
}
if c.clientSecret != clientSecret {
if c.hashRule.value != clientSecret {
return fmt.Errorf("fail to match secret,now: %s,hope: %s,client id is %s", clientSecret, c.hashRule.value, c.clientId)
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion drivers/plugins/oauth2/authorize.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (e *executor) Authorize(ctx http_context.IHttpContext, client oauth2.IClien
break
}
}
if !matchScope {
if len(e.cfg.Scopes) > 0 && !matchScope {
return nil, fmt.Errorf("invalid scope,client id %s", client.ClientID())
}

Expand Down
9 changes: 8 additions & 1 deletion drivers/plugins/oauth2/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package oauth2

import (
"sync"

"github.com/eolinker/apinto/drivers"
"github.com/eolinker/eosc"
)

Expand Down Expand Up @@ -28,5 +31,9 @@ type Config struct {
}

func Create(id, name string, conf *Config, workers map[eosc.RequireId]eosc.IWorker) (eosc.IWorker, error) {
return nil, nil
return &executor{
WorkerBase: drivers.Worker(id, name),
cfg: conf,
once: sync.Once{},
}, nil
}
26 changes: 12 additions & 14 deletions drivers/plugins/oauth2/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,24 +52,29 @@ func (e *executor) DoHttpFilter(ctx http_service.IHttpContext, next eocontext.IC
}
return
}
defer func() {
if err != nil {
log.Error(err)
type errResp struct {
Message string `json:"message"`
}
msg, _ := json.Marshal(errResp{Message: "Unauthorized"})
ctx.Response().SetBody(msg)
ctx.Response().SetStatus(http.StatusUnauthorized, "unauthorized")
}
}()
client, has := oauth2.GetClient(clientId)
if !has {
err = fmt.Errorf("invalid client id")
ctx.Response().SetBody([]byte(err.Error()))
ctx.Response().SetStatus(http.StatusForbidden, "forbidden")
return
}

if strings.ToUpper(ctx.Request().URI().Scheme()) != "HTTPS" && !e.cfg.AcceptHttpIfAlreadyTerminated {
err = fmt.Errorf("invalid scheme")
ctx.Response().SetBody([]byte(err.Error()))
ctx.Response().SetStatus(http.StatusForbidden, "forbidden")
return
}
if client.Expire() > 0 && client.Expire() < time.Now().Unix() {
err = fmt.Errorf("client id is expired")
ctx.Response().SetBody([]byte("client id is expired"))
ctx.Response().SetStatus(http.StatusForbidden, "forbidden")
return
}

Expand All @@ -84,14 +89,7 @@ func (e *executor) DoHttpFilter(ctx http_service.IHttpContext, next eocontext.IC
data, err = e.Token(ctx, client, params)
}
if err != nil {
log.Error(err)
type errResp struct {
Message string `json:"message"`
}
msg, _ := json.Marshal(errResp{Message: "Unauthorized"})
ctx.Response().SetBody(msg)
ctx.Response().SetStatus(http.StatusUnauthorized, "unauthorized")
return err
return
}
ctx.Response().SetBody(data)
ctx.Response().SetStatus(http.StatusOK, "ok")
Expand Down

0 comments on commit aacd9c0

Please sign in to comment.