forked from casdoor/casdoor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support webauthn (casdoor#407)
* feat: support webauthn * Update init.go * Update user_webauthn.go * Update UserEditPage.js * Update WebauthnCredentialTable.js * Update LoginPage.js Co-authored-by: Gucheng <[email protected]>
- Loading branch information
1 parent
208dc11
commit 7f3b250
Showing
23 changed files
with
622 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
// 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" | ||
"io/ioutil" | ||
|
||
"github.com/casdoor/casdoor/object" | ||
"github.com/casdoor/casdoor/util" | ||
"github.com/duo-labs/webauthn/protocol" | ||
"github.com/duo-labs/webauthn/webauthn" | ||
) | ||
|
||
// @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 { | ||
c.ResponseError("Please login first.") | ||
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() | ||
} | ||
|
||
// @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 { | ||
c.ResponseError("Please login first.") | ||
return | ||
} | ||
sessionObj := c.GetSession("registration") | ||
sessionData, ok := sessionObj.(webauthn.SessionData) | ||
if !ok { | ||
c.ResponseError("Please call WebAuthnSignupBegin first") | ||
return | ||
} | ||
c.Ctx.Request.Body = ioutil.NopCloser(bytes.NewBuffer(c.Ctx.Input.RequestBody)) | ||
|
||
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() | ||
} | ||
|
||
// @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 { | ||
c.ResponseError("Please Giveout Owner and Username.") | ||
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() | ||
} | ||
|
||
// @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() { | ||
webauthnObj := object.GetWebAuthnObject(c.Ctx.Request.Host) | ||
sessionObj := c.GetSession("authentication") | ||
sessionData, ok := sessionObj.(webauthn.SessionData) | ||
if !ok { | ||
c.ResponseError("Please call WebAuthnSigninBegin first") | ||
return | ||
} | ||
c.Ctx.Request.Body = ioutil.NopCloser(bytes.NewBuffer(c.Ctx.Input.RequestBody)) | ||
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) | ||
c.ResponseOk(userId) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
// Copyright 2022 The casbin 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 object | ||
|
||
import ( | ||
"encoding/base64" | ||
"net/url" | ||
"strings" | ||
|
||
"github.com/astaxie/beego" | ||
"github.com/duo-labs/webauthn/protocol" | ||
"github.com/duo-labs/webauthn/webauthn" | ||
) | ||
|
||
func GetWebAuthnObject(host string) *webauthn.WebAuthn { | ||
var err error | ||
|
||
origin := beego.AppConfig.String("origin") | ||
if origin == "" { | ||
_, origin = getOriginFromHost(host) | ||
} | ||
|
||
localUrl, err := url.Parse(origin) | ||
if err != nil { | ||
panic("error when parsing origin:" + err.Error()) | ||
} | ||
|
||
webAuthn, err := webauthn.New(&webauthn.Config{ | ||
RPDisplayName: beego.AppConfig.String("appname"), // Display Name for your site | ||
RPID: strings.Split(localUrl.Host, ":")[0], // Generally the domain name for your site, it's ok because splits cannot return empty array | ||
RPOrigin: origin, // The origin URL for WebAuthn requests | ||
// RPIcon: "https://duo.com/logo.png", // Optional icon URL for your site | ||
}) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return webAuthn | ||
} | ||
|
||
// implementation of webauthn.User interface | ||
func (u *User) WebAuthnID() []byte { | ||
return []byte(u.GetId()) | ||
} | ||
|
||
func (u *User) WebAuthnName() string { | ||
return u.Name | ||
} | ||
|
||
func (u *User) WebAuthnDisplayName() string { | ||
return u.DisplayName | ||
} | ||
|
||
func (u *User) WebAuthnCredentials() []webauthn.Credential { | ||
return u.WebauthnCredentials | ||
} | ||
|
||
func (u *User) WebAuthnIcon() string { | ||
return u.Avatar | ||
} | ||
|
||
// CredentialExcludeList returns a CredentialDescriptor array filled with all the user's credentials | ||
func (u *User) CredentialExcludeList() []protocol.CredentialDescriptor { | ||
credentials := u.WebAuthnCredentials() | ||
credentialExcludeList := []protocol.CredentialDescriptor{} | ||
for _, cred := range credentials { | ||
descriptor := protocol.CredentialDescriptor{ | ||
Type: protocol.PublicKeyCredentialType, | ||
CredentialID: cred.ID, | ||
} | ||
credentialExcludeList = append(credentialExcludeList, descriptor) | ||
} | ||
|
||
return credentialExcludeList | ||
} | ||
|
||
func (u *User) AddCredentials(credential webauthn.Credential, isGlobalAdmin bool) bool { | ||
u.WebauthnCredentials = append(u.WebauthnCredentials, credential) | ||
return UpdateUser(u.GetId(), u, []string{"webauthnCredentials"}, isGlobalAdmin) | ||
} | ||
|
||
func (u *User) DeleteCredentials(credentialIdBase64 string) bool { | ||
for i, credential := range u.WebauthnCredentials { | ||
if base64.StdEncoding.EncodeToString(credential.ID) == credentialIdBase64 { | ||
u.WebauthnCredentials = append(u.WebauthnCredentials[0:i], u.WebauthnCredentials[i+1:]...) | ||
return UpdateUserForAllFields(u.GetId(), u) | ||
} | ||
} | ||
return false | ||
} |
Oops, something went wrong.