diff --git a/internal/client/client.go b/internal/client/client.go
index aadc5a0..fc6df78 100644
--- a/internal/client/client.go
+++ b/internal/client/client.go
@@ -2,33 +2,82 @@ package client
import (
"context"
+ _ "embed"
+ "encoding/json"
"github.com/google/uuid"
+ "github.com/loveuer/nf"
+ "github.com/loveuer/nf/nft/log"
+ "github.com/loveuer/nf/nft/resp"
"golang.org/x/oauth2"
- "golang.org/x/oauth2/authhandler"
+ "net/http"
+ "uauth/internal/tool"
)
+//go:embed login.html
+var page string
+
var (
- State = uuid.New().String()[:8]
+ config = oauth2.Config{
+ ClientID: "test",
+ ClientSecret: "test",
+ Endpoint: oauth2.Endpoint{
+ AuthURL: "http://localhost:8080/oauth/v2/authorize",
+ TokenURL: "http://localhost:8080/oauth/v2/token",
+ },
+ RedirectURL: "http://localhost:18080/oauth/v2/redirect",
+ Scopes: []string{"test"},
+ }
+ state = uuid.New().String()[:8]
)
func Run(ctx context.Context) error {
- oauth2.NewClient(ctx, authhandler.TokenSource(
- ctx,
- &oauth2.Config{
- ClientID: "",
- ClientSecret: "",
- Endpoint: oauth2.Endpoint{
- AuthURL: "",
- DeviceAuthURL: "",
- TokenURL: "",
- AuthStyle: 0,
- },
- RedirectURL: "",
- Scopes: nil,
- },
- State,
- func(authCodeURL string) (code string, state string, err error) {
+ app := nf.New()
+ app.Get("/login", handleLogin)
+ app.Get("/oauth/v2/redirect", handleRedirect)
- },
- ))
+ go func() {
+ <-ctx.Done()
+ _ = app.Shutdown(tool.Timeout(2))
+ }()
+
+ return app.Run(":18080")
+}
+
+func handleLogin(c *nf.Ctx) error {
+ if c.Query("oauth") != "" {
+ return c.Redirect(config.AuthCodeURL(state), http.StatusFound)
+ }
+
+ return c.HTML(page)
+}
+
+func handleRedirect(c *nf.Ctx) error {
+ type Req struct {
+ State string `query:"state"`
+ Code string `query:"code"`
+ }
+
+ var (
+ err error
+ req = new(Req)
+ token *oauth2.Token
+ )
+
+ if err = c.QueryParser(req); err != nil {
+ return resp.Resp400(c, err.Error())
+ }
+
+ if req.State != state {
+ return resp.Resp400(c, "invalid state")
+ }
+
+ if token, err = config.Exchange(c.Context(), req.Code); err != nil {
+ log.Error("[C] oauth config exchange err: %s", err.Error())
+ return resp.Resp500(c, err.Error())
+ }
+
+ bs, _ := json.Marshal(token)
+ log.Info("[C] oauth finally token =\n%s", string(bs))
+
+ return resp.Resp200(c, token)
}
diff --git a/internal/client/login.html b/internal/client/login.html
index 0dc46b3..bb57e1d 100644
--- a/internal/client/login.html
+++ b/internal/client/login.html
@@ -45,7 +45,7 @@
type="submit"
value="登录"
/>
- 使用 Pro 账号登录
+ 使用 OAuth V2 账号登录