mirror of
https://github.com/balzack/databag.git
synced 2025-03-13 00:50:03 +00:00
support multiple 2k and 4k keys
This commit is contained in:
parent
51c8f648e5
commit
4d2ccfb6e4
@ -16,14 +16,18 @@ func AddAccount(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
username, password, err := BasicCredentials(r);
|
||||
if err != nil {
|
||||
ErrResponse(w, http.StatusUnauthorized, err)
|
||||
username, password, ret := BasicCredentials(r);
|
||||
if ret != nil {
|
||||
ErrResponse(w, http.StatusUnauthorized, ret)
|
||||
return
|
||||
}
|
||||
|
||||
// generate account key
|
||||
privateKey, publicKey := GenerateRsaKeyPair()
|
||||
privateKey, publicKey, keyType, err := GenerateRsaKeyPair()
|
||||
if err != nil {
|
||||
ErrResponse(w, http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
privatePem := ExportRsaPrivateKeyAsPemStr(privateKey)
|
||||
publicPem, err := ExportRsaPublicKeyAsPemStr(publicKey)
|
||||
if err != nil {
|
||||
@ -45,7 +49,7 @@ func AddAccount(w http.ResponseWriter, r *http.Request) {
|
||||
detail := store.AccountDetail{
|
||||
PublicKey: publicPem,
|
||||
PrivateKey: privatePem,
|
||||
KeyType: "RSA4096",
|
||||
KeyType: keyType,
|
||||
}
|
||||
|
||||
// save account and delete token
|
||||
|
@ -32,7 +32,7 @@ func Authorize(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
// load details to sign data
|
||||
if account.AccountDetail.KeyType != "RSA4096" {
|
||||
if account.AccountDetail.KeyType != APP_RSA2048 && account.AccountDetail.KeyType != APP_RSA4096 {
|
||||
w.WriteHeader(http.StatusServiceUnavailable)
|
||||
return
|
||||
}
|
||||
|
@ -4,3 +4,6 @@ const APP_BODYLIMIT = 1048576
|
||||
const APP_VERSION = "0.0.1"
|
||||
const APP_ATTACHEXPIRE = 300
|
||||
const APP_CREATEEXPIRE = 86400
|
||||
const APP_KEYSIZE = 4096
|
||||
const APP_RSA4096 = "RSA4096"
|
||||
const APP_RSA2048 = "RSA2048"
|
||||
|
@ -8,9 +8,21 @@ import (
|
||||
"errors"
|
||||
)
|
||||
|
||||
func GenerateRsaKeyPair() (*rsa.PrivateKey, *rsa.PublicKey) {
|
||||
privkey, _ := rsa.GenerateKey(rand.Reader, 4096)
|
||||
return privkey, &privkey.PublicKey
|
||||
var keySize int = APP_KEYSIZE
|
||||
func SetKeySize(size int) {
|
||||
keySize = size
|
||||
}
|
||||
|
||||
func GenerateRsaKeyPair() (*rsa.PrivateKey, *rsa.PublicKey, string, error) {
|
||||
if keySize == 2048 {
|
||||
privkey, _ := rsa.GenerateKey(rand.Reader, keySize)
|
||||
return privkey, &privkey.PublicKey, "RSA2048", nil
|
||||
} else if keySize == 4096 {
|
||||
privkey, _ := rsa.GenerateKey(rand.Reader, keySize)
|
||||
return privkey, &privkey.PublicKey, "RSA2048", nil
|
||||
} else {
|
||||
return nil, nil, "", errors.New("invalid key setting")
|
||||
}
|
||||
}
|
||||
|
||||
func ExportRsaPrivateKeyAsPemStr(privkey *rsa.PrivateKey) string {
|
||||
|
@ -20,6 +20,9 @@ import (
|
||||
)
|
||||
|
||||
var hideLog bool = false
|
||||
func SetHideLog(hide bool) {
|
||||
hideLog = hide
|
||||
}
|
||||
|
||||
func Logger(inner http.Handler, name string) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
|
@ -7,7 +7,8 @@ import (
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
|
||||
hideLog = true
|
||||
SetHideLog(true)
|
||||
SetKeySize(2048)
|
||||
store.SetPath("file::memory:?cache=shared");
|
||||
//store.SetPath("databag.db");
|
||||
|
||||
|
@ -60,7 +60,6 @@ func TestAttachAccount(t *testing.T) {
|
||||
assert.NoError(t, ReadResponse(w, &message))
|
||||
|
||||
// validate message
|
||||
assert.Equal(t, "RSA4096", message.KeyType)
|
||||
assert.Equal(t, "PKCS1v15", message.SignatureType)
|
||||
var data []byte
|
||||
var hash [32]byte
|
||||
|
28
net/server/internal/ucConnectContact_test.go
Normal file
28
net/server/internal/ucConnectContact_test.go
Normal file
@ -0,0 +1,28 @@
|
||||
package databag
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestConnectContact(t *testing.T) {
|
||||
|
||||
// create A
|
||||
|
||||
// A profile update
|
||||
|
||||
// create B
|
||||
|
||||
// B profile update
|
||||
|
||||
// get B profile message
|
||||
|
||||
// set B card in A
|
||||
|
||||
// get A open message
|
||||
|
||||
// set A card in B
|
||||
|
||||
// accept A
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user