Modify: create Web Authn instance when needed

This commit is contained in:
HFO4
2020-02-23 15:50:41 +08:00
parent ce2a70df68
commit ef42ec3927
5 changed files with 34 additions and 35 deletions

View File

@@ -2,26 +2,15 @@ package authn
import (
model "github.com/HFO4/cloudreve/models"
"github.com/HFO4/cloudreve/pkg/util"
"github.com/duo-labs/webauthn/webauthn"
"sync"
)
var AuthnInstance *webauthn.WebAuthn
var Lock sync.RWMutex
// Init 初始化webauthn
func Init() {
Lock.Lock()
defer Lock.Unlock()
var err error
// NewAuthnInstance 新建Authn实例
func NewAuthnInstance() (*webauthn.WebAuthn, error) {
base := model.GetSiteURL()
AuthnInstance, err = webauthn.New(&webauthn.Config{
return webauthn.New(&webauthn.Config{
RPDisplayName: model.GetSettingByName("siteName"), // Display Name for your site
RPID: base.Hostname(), // Generally the FQDN for your site
RPOrigin: base.String(), // The origin URL for WebAuthn requests
})
if err != nil {
util.Log().Error("无法初始化WebAuthn, %s", err)
}
}

View File

@@ -10,8 +10,7 @@ func TestInit(t *testing.T) {
asserts := assert.New(t)
cache.Set("setting_siteURL", "http://cloudreve.org", 0)
cache.Set("setting_siteName", "Cloudreve", 0)
asserts.NotPanics(func() {
Init()
})
asserts.NotNil(AuthnInstance)
res, err := NewAuthnInstance()
asserts.NotNil(res)
asserts.NoError(err)
}