Changes
This commit is contained in:
parent
7db9c374cc
commit
9eac960763
26 changed files with 3119 additions and 266 deletions
|
@ -2,15 +2,16 @@ package device
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"nutfactory.org/Matrix/utils/database"
|
||||
)
|
||||
|
||||
func CreateDevice(device *Device, userId string) (err error) {
|
||||
sqlStmt := fmt.Sprintf(`INSERT INTO device
|
||||
(id, name, userId)
|
||||
(id, name, accessToken, userId)
|
||||
VALUES
|
||||
(?, ?, ?)`)
|
||||
(?, ?, ?, ?)`)
|
||||
|
||||
tx, err := database.DB.Begin()
|
||||
if err != nil {
|
||||
|
@ -23,7 +24,7 @@ func CreateDevice(device *Device, userId string) (err error) {
|
|||
}
|
||||
defer stmt.Close()
|
||||
|
||||
_, err = stmt.Exec(device.Id, device.Name, userId)
|
||||
_, err = stmt.Exec(device.Id, device.Name, device.AccessToken, userId)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
@ -32,7 +33,7 @@ func CreateDevice(device *Device, userId string) (err error) {
|
|||
}
|
||||
|
||||
func ReadDevice(id string) (foundDevice *Device, err error) {
|
||||
queryStmt := fmt.Sprintf(`SELECT id, name
|
||||
queryStmt := fmt.Sprintf(`SELECT id, name, accessToken
|
||||
FROM device
|
||||
WHERE id = '%s'`, id)
|
||||
|
||||
|
@ -45,7 +46,31 @@ func ReadDevice(id string) (foundDevice *Device, err error) {
|
|||
|
||||
if rows.Next() {
|
||||
foundDevice = &Device{}
|
||||
err = rows.Scan(&foundDevice.Id, &foundDevice.Name)
|
||||
err = rows.Scan(&foundDevice.Id, &foundDevice.Name, &foundDevice.AccessToken)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
foundDevice.Keys, err = ReadKeysForDevice(foundDevice.Id)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func ReadDeviceFromAccessToken(accessToken string) (foundDevice *Device, err error) {
|
||||
queryStmt := fmt.Sprintf(`SELECT id, name, accessToken
|
||||
FROM device
|
||||
WHERE accessToken = '%s'`, accessToken)
|
||||
log.Printf(queryStmt)
|
||||
rows, err := database.DB.Query(queryStmt)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
defer rows.Close()
|
||||
|
||||
if rows.Next() {
|
||||
foundDevice = &Device{}
|
||||
err = rows.Scan(&foundDevice.Id, &foundDevice.Name, &foundDevice.AccessToken)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
@ -56,7 +81,7 @@ func ReadDevice(id string) (foundDevice *Device, err error) {
|
|||
}
|
||||
|
||||
func ReadDevicesForUser(userId string) (devices map[string]*Device, err error) {
|
||||
queryStmt := fmt.Sprintf(`SELECT id, name
|
||||
queryStmt := fmt.Sprintf(`SELECT id, name, accessToken
|
||||
FROM device
|
||||
WHERE userId = '%s'`, userId)
|
||||
|
||||
|
@ -71,7 +96,7 @@ func ReadDevicesForUser(userId string) (devices map[string]*Device, err error) {
|
|||
|
||||
for rows.Next() {
|
||||
foundDevice := &Device{}
|
||||
err = rows.Scan(&foundDevice.Id, &foundDevice.Name)
|
||||
err = rows.Scan(&foundDevice.Id, &foundDevice.Name, &foundDevice.AccessToken)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
@ -82,10 +107,10 @@ func ReadDevicesForUser(userId string) (devices map[string]*Device, err error) {
|
|||
return
|
||||
}
|
||||
|
||||
func UpdateDevice(device *Device, userId string) (err error) {
|
||||
func UpdateDevice(device *Device) (err error) {
|
||||
sqlStmt := fmt.Sprintf(`UPDATE device SET
|
||||
name = ?,
|
||||
userId = ?
|
||||
accessToken = ?
|
||||
WHERE id = ?`)
|
||||
|
||||
tx, err := database.DB.Begin()
|
||||
|
@ -99,7 +124,7 @@ func UpdateDevice(device *Device, userId string) (err error) {
|
|||
}
|
||||
defer stmt.Close()
|
||||
|
||||
_, err = stmt.Exec(device.Name, userId, device.Id)
|
||||
_, err = stmt.Exec(device.Name, device.AccessToken, device.Id)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
|
|
@ -3,5 +3,18 @@ package device
|
|||
type Key struct {
|
||||
Id string `json:"id,omitempty"`
|
||||
Type string `json:"type,omitempty"`
|
||||
Key string `json:"key,omitempty"`
|
||||
Key []byte `json:"key,omitempty"`
|
||||
}
|
||||
|
||||
type serverKeys struct {
|
||||
ServerName string `json:"server_name,omitempty"`
|
||||
VerifyKeys map[string]verifyKey `json:"verify_keys,omitempty"`
|
||||
OldVerifyKeys map[string]verifyKey `json:"old_verify_keys,omitempty"`
|
||||
Signatures map[string]map[string]string `json:"signatures,omitempty"`
|
||||
ValidUntil int64 `json:"valid_until_ts,omitempty"`
|
||||
}
|
||||
|
||||
type verifyKey struct {
|
||||
Key string `json:"key,omitempty"`
|
||||
Expired int64 `json:"expired_ts,omitempty"`
|
||||
}
|
||||
|
|
76
entities/device/keyController.go
Normal file
76
entities/device/keyController.go
Normal file
|
@ -0,0 +1,76 @@
|
|||
package device
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"nutfactory.org/Matrix/config"
|
||||
"nutfactory.org/Matrix/utils"
|
||||
)
|
||||
|
||||
func InitServerSigningKey() (err error) {
|
||||
publicKey, privateKey, err := utils.GenerateKeyPair()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
config.PublicKey = publicKey
|
||||
config.PrivateKey = privateKey
|
||||
config.KeyId = "ed25519:1"
|
||||
return
|
||||
}
|
||||
|
||||
func GetServerSigningKeyHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if config.PublicKey == nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
if err := json.NewEncoder(w).Encode(utils.ErrorResponse{ErrorMessage: "Server Signing Key Missing"}); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return
|
||||
}
|
||||
response := serverKeys{
|
||||
ServerName: config.Homeserver,
|
||||
VerifyKeys: make(map[string]verifyKey),
|
||||
}
|
||||
response.VerifyKeys[config.KeyId] = verifyKey{Key: string(config.PublicKey)}
|
||||
content, err := json.Marshal(response)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
if err := json.NewEncoder(w).Encode(utils.ErrorResponse{ErrorMessage: fmt.Sprintf("Error creating Signatures: %s", err)}); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
response.Signatures = utils.SignContent(content)
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
if err := json.NewEncoder(w).Encode(response); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func getVerifyKey(server string, id string) (key []byte, err error) {
|
||||
if val, ok := config.VerifyKeys[server][id]; ok {
|
||||
key = val
|
||||
} else {
|
||||
httpString := "https"
|
||||
requestUrl := fmt.Sprintf("%s://%s/_matrix/key/v2/server", httpString, server)
|
||||
var res *http.Response
|
||||
res, err = http.Get(requestUrl)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
serverKeyRes := serverKeys{}
|
||||
decoder := json.NewDecoder(res.Body)
|
||||
err = decoder.Decode(&serverKeyRes)
|
||||
config.VerifyKeys[server] = make(map[string][]byte)
|
||||
for keyId, verifyKey := range serverKeyRes.VerifyKeys {
|
||||
config.VerifyKeys[server][keyId] = []byte(verifyKey.Key)
|
||||
if id == keyId {
|
||||
key = []byte(verifyKey.Key)
|
||||
}
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue