2022-07-12 20:06:01 +08:00
|
|
|
// Copyright 2022 The Casdoor Authors. All Rights Reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package controllers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2022-10-03 15:10:48 +08:00
|
|
|
"fmt"
|
2022-08-09 16:50:49 +08:00
|
|
|
"io"
|
2022-07-12 20:06:01 +08:00
|
|
|
|
|
|
|
"github.com/casdoor/casdoor/object"
|
|
|
|
"github.com/casdoor/casdoor/util"
|
|
|
|
"github.com/duo-labs/webauthn/protocol"
|
|
|
|
"github.com/duo-labs/webauthn/webauthn"
|
|
|
|
)
|
|
|
|
|
2022-08-09 16:50:49 +08:00
|
|
|
// WebAuthnSignupBegin
|
2022-07-12 20:06:01 +08:00
|
|
|
// @Title WebAuthnSignupBegin
|
|
|
|
// @Tag User API
|
|
|
|
// @Description WebAuthn Registration Flow 1st stage
|
|
|
|
// @Success 200 {object} protocol.CredentialCreation The CredentialCreationOptions object
|
|
|
|
// @router /webauthn/signup/begin [get]
|
|
|
|
func (c *ApiController) WebAuthnSignupBegin() {
|
|
|
|
webauthnObj := object.GetWebAuthnObject(c.Ctx.Request.Host)
|
|
|
|
user := c.getCurrentUser()
|
|
|
|
if user == nil {
|
2022-10-23 15:16:24 +08:00
|
|
|
c.ResponseError(c.T("LoginErr.LoginFirst"))
|
2022-07-12 20:06:01 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
registerOptions := func(credCreationOpts *protocol.PublicKeyCredentialCreationOptions) {
|
|
|
|
credCreationOpts.CredentialExcludeList = user.CredentialExcludeList()
|
|
|
|
}
|
|
|
|
options, sessionData, err := webauthnObj.BeginRegistration(
|
|
|
|
user,
|
|
|
|
registerOptions,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.SetSession("registration", *sessionData)
|
|
|
|
c.Data["json"] = options
|
|
|
|
c.ServeJSON()
|
|
|
|
}
|
|
|
|
|
2022-08-09 16:50:49 +08:00
|
|
|
// WebAuthnSignupFinish
|
2022-07-12 20:06:01 +08:00
|
|
|
// @Title WebAuthnSignupFinish
|
|
|
|
// @Tag User API
|
|
|
|
// @Description WebAuthn Registration Flow 2nd stage
|
|
|
|
// @Param body body protocol.CredentialCreationResponse true "authenticator attestation Response"
|
|
|
|
// @Success 200 {object} Response "The Response object"
|
|
|
|
// @router /webauthn/signup/finish [post]
|
|
|
|
func (c *ApiController) WebAuthnSignupFinish() {
|
|
|
|
webauthnObj := object.GetWebAuthnObject(c.Ctx.Request.Host)
|
|
|
|
user := c.getCurrentUser()
|
|
|
|
if user == nil {
|
2022-10-23 15:16:24 +08:00
|
|
|
c.ResponseError(c.T("LoginErr.LoginFirst"))
|
2022-07-12 20:06:01 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
sessionObj := c.GetSession("registration")
|
|
|
|
sessionData, ok := sessionObj.(webauthn.SessionData)
|
|
|
|
if !ok {
|
2022-10-23 15:16:24 +08:00
|
|
|
c.ResponseError(c.T("AuthErr.CallWebAuthnSigninBegin"))
|
2022-07-12 20:06:01 +08:00
|
|
|
return
|
|
|
|
}
|
2022-08-09 16:50:49 +08:00
|
|
|
c.Ctx.Request.Body = io.NopCloser(bytes.NewBuffer(c.Ctx.Input.RequestBody))
|
2022-07-12 20:06:01 +08:00
|
|
|
|
|
|
|
credential, err := webauthnObj.FinishRegistration(user, sessionData, c.Ctx.Request)
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
isGlobalAdmin := c.IsGlobalAdmin()
|
|
|
|
user.AddCredentials(*credential, isGlobalAdmin)
|
|
|
|
c.ResponseOk()
|
|
|
|
}
|
|
|
|
|
2022-08-09 16:50:49 +08:00
|
|
|
// WebAuthnSigninBegin
|
2022-07-12 20:06:01 +08:00
|
|
|
// @Title WebAuthnSigninBegin
|
|
|
|
// @Tag Login API
|
|
|
|
// @Description WebAuthn Login Flow 1st stage
|
|
|
|
// @Param owner query string true "owner"
|
|
|
|
// @Param name query string true "name"
|
|
|
|
// @Success 200 {object} protocol.CredentialAssertion The CredentialAssertion object
|
|
|
|
// @router /webauthn/signin/begin [get]
|
|
|
|
func (c *ApiController) WebAuthnSigninBegin() {
|
|
|
|
webauthnObj := object.GetWebAuthnObject(c.Ctx.Request.Host)
|
|
|
|
userOwner := c.Input().Get("owner")
|
|
|
|
userName := c.Input().Get("name")
|
|
|
|
user := object.GetUserByFields(userOwner, userName)
|
|
|
|
if user == nil {
|
2022-10-23 15:16:24 +08:00
|
|
|
c.ResponseError(fmt.Sprintf(c.T("UserErr.DoNotExistInOrg"), userOwner, userName))
|
2022-07-12 20:06:01 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
options, sessionData, err := webauthnObj.BeginLogin(user)
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.SetSession("authentication", *sessionData)
|
|
|
|
c.Data["json"] = options
|
|
|
|
c.ServeJSON()
|
|
|
|
}
|
|
|
|
|
2022-08-09 16:50:49 +08:00
|
|
|
// WebAuthnSigninFinish
|
2022-07-12 20:06:01 +08:00
|
|
|
// @Title WebAuthnSigninBegin
|
|
|
|
// @Tag Login API
|
|
|
|
// @Description WebAuthn Login Flow 2nd stage
|
|
|
|
// @Param body body protocol.CredentialAssertionResponse true "authenticator assertion Response"
|
|
|
|
// @Success 200 {object} Response "The Response object"
|
|
|
|
// @router /webauthn/signin/finish [post]
|
|
|
|
func (c *ApiController) WebAuthnSigninFinish() {
|
2022-10-01 11:10:55 +08:00
|
|
|
responseType := c.Input().Get("responseType")
|
2022-07-12 20:06:01 +08:00
|
|
|
webauthnObj := object.GetWebAuthnObject(c.Ctx.Request.Host)
|
|
|
|
sessionObj := c.GetSession("authentication")
|
|
|
|
sessionData, ok := sessionObj.(webauthn.SessionData)
|
|
|
|
if !ok {
|
2022-10-23 15:16:24 +08:00
|
|
|
c.ResponseError(c.T("AuthErr.CallWebAuthnSigninBegin"))
|
2022-07-12 20:06:01 +08:00
|
|
|
return
|
|
|
|
}
|
2022-08-09 16:50:49 +08:00
|
|
|
c.Ctx.Request.Body = io.NopCloser(bytes.NewBuffer(c.Ctx.Input.RequestBody))
|
2022-07-12 20:06:01 +08:00
|
|
|
userId := string(sessionData.UserID)
|
|
|
|
user := object.GetUser(userId)
|
|
|
|
_, err := webauthnObj.FinishLogin(user, sessionData, c.Ctx.Request)
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.SetSessionUsername(userId)
|
|
|
|
util.LogInfo(c.Ctx, "API: [%s] signed in", userId)
|
2022-10-01 11:10:55 +08:00
|
|
|
|
|
|
|
application := object.GetApplicationByUser(user)
|
|
|
|
var form RequestForm
|
|
|
|
form.Type = responseType
|
|
|
|
resp := c.HandleLoggedIn(application, user, &form)
|
|
|
|
c.Data["json"] = resp
|
|
|
|
c.ServeJSON()
|
2022-07-12 20:06:01 +08:00
|
|
|
}
|