feat: 添加 password 模式
This commit is contained in:
@ -32,7 +32,8 @@ var (
|
||||
|
||||
func Run(ctx context.Context) error {
|
||||
app := nf.New()
|
||||
app.Get("/login", handleLogin)
|
||||
app.Get("/login", handleLoginPage)
|
||||
app.Post("/login", handleLoginAction)
|
||||
app.Get("/oauth/v2/redirect", handleRedirect)
|
||||
|
||||
go func() {
|
||||
@ -43,9 +44,38 @@ func Run(ctx context.Context) error {
|
||||
return app.Run(":18080")
|
||||
}
|
||||
|
||||
func handleLogin(c *nf.Ctx) error {
|
||||
func handleLoginAction(c *nf.Ctx) error {
|
||||
type Req struct {
|
||||
Username string `form:"username"`
|
||||
Password string `form:"password"`
|
||||
}
|
||||
|
||||
var (
|
||||
err error
|
||||
req = new(Req)
|
||||
token *oauth2.Token
|
||||
)
|
||||
|
||||
if err = c.BodyParser(req); err != nil {
|
||||
return c.Status(http.StatusBadRequest).SendString(err.Error())
|
||||
}
|
||||
|
||||
log.Info("[C] password mode: username = %s, password = %s", req.Username, req.Password)
|
||||
|
||||
if token, err = config.PasswordCredentialsToken(c.Context(), req.Username, req.Password); err != nil {
|
||||
log.Error("[C] config do password token err = %s", err)
|
||||
return c.Status(http.StatusBadRequest).SendString(err.Error())
|
||||
}
|
||||
|
||||
bs, _ := json.Marshal(token)
|
||||
log.Info("[C] oauth finally token =\n%s", string(bs))
|
||||
|
||||
return resp.Resp200(c, token)
|
||||
}
|
||||
|
||||
func handleLoginPage(c *nf.Ctx) error {
|
||||
if c.Query("oauth") != "" {
|
||||
uri := config.AuthCodeURL(state)
|
||||
uri := config.AuthCodeURL(state, oauth2.ApprovalForce)
|
||||
log.Info("[C] oauth config client_secret = %s", config.ClientSecret)
|
||||
log.Info("[C] redirect to oauth2 server uri = %s", uri)
|
||||
return c.Redirect(uri, http.StatusFound)
|
||||
|
@ -20,7 +20,7 @@
|
||||
<body>
|
||||
<div>
|
||||
<h3>这里是 xx 产品登录页面</h3>
|
||||
<form>
|
||||
<form action="/login" method="post">
|
||||
<fieldset>
|
||||
<label>
|
||||
Username
|
||||
@ -41,10 +41,11 @@
|
||||
</label>
|
||||
</fieldset>
|
||||
|
||||
<input
|
||||
<button
|
||||
data-tooltip="采用 OAuth V2 登录(密码模式)"
|
||||
type="submit"
|
||||
value="登录"
|
||||
/>
|
||||
|
||||
>登录</button>
|
||||
<a href="/login?oauth=true">使用 OAuth V2 账号登录</a>
|
||||
</form>
|
||||
</div>
|
||||
|
Reference in New Issue
Block a user