Changes
This commit is contained in:
parent
7db9c374cc
commit
9eac960763
26 changed files with 3119 additions and 266 deletions
|
@ -34,7 +34,8 @@ func initDeviceTable() (err error) {
|
|||
log.Printf("Init Device Table")
|
||||
statement, err := DB.Prepare(`CREATE TABLE IF NOT EXISTS device (
|
||||
id TEXT PRIMARY KEY,
|
||||
name TEXT,
|
||||
name TEXT,
|
||||
accessToken TEXT,
|
||||
userId TEXT
|
||||
)`)
|
||||
if err != nil {
|
||||
|
@ -137,10 +138,35 @@ func initEventTable() (err error) {
|
|||
id TEXT PRIMARY KEY,
|
||||
roomId TEXT,
|
||||
txnId TEXT,
|
||||
sender TEXT,
|
||||
origin TEXT,
|
||||
timestamp INTEGER,
|
||||
eventType TEXT,
|
||||
content TEXT,
|
||||
parentId TEXT,
|
||||
depth INTEGER
|
||||
stateKey TEXT,
|
||||
content TEXT,
|
||||
depth INTEGER,
|
||||
hash TEXT,
|
||||
signature TEXT
|
||||
)`)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
statement.Exec()
|
||||
|
||||
statement, err = DB.Prepare(`CREATE TABLE IF NOT EXISTS parent (
|
||||
eventId TEXT,
|
||||
parentId TEXT,
|
||||
PRIMARY KEY (eventId, parentId)
|
||||
)`)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
statement.Exec()
|
||||
|
||||
statement, err = DB.Prepare(`CREATE TABLE IF NOT EXISTS authEvent (
|
||||
eventId TEXT,
|
||||
authEventId TEXT,
|
||||
PRIMARY KEY (eventId, authEventId)
|
||||
)`)
|
||||
if err != nil {
|
||||
return
|
||||
|
@ -202,7 +228,12 @@ func initRoomTable() (err error) {
|
|||
log.Printf("Init Room Table")
|
||||
statement, err := DB.Prepare(`CREATE TABLE IF NOT EXISTS room (
|
||||
id TEXT PRIMARY KEY,
|
||||
version TEXT
|
||||
version TEXT,
|
||||
visibility TEXT,
|
||||
name TEXT,
|
||||
topic TEXT,
|
||||
isDirect INT,
|
||||
federated INT
|
||||
)`)
|
||||
if err != nil {
|
||||
return
|
||||
|
@ -211,6 +242,7 @@ func initRoomTable() (err error) {
|
|||
statement, err = DB.Prepare(`CREATE TABLE IF NOT EXISTS roomMember (
|
||||
userId TEXT,
|
||||
roomId TEXT,
|
||||
server TEXT,
|
||||
PRIMARY KEY (userId, roomId)
|
||||
)`)
|
||||
if err != nil {
|
||||
|
|
|
@ -1,11 +1,14 @@
|
|||
package utils
|
||||
|
||||
import (
|
||||
"crypto/ed25519"
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"nutfactory.org/Matrix/config"
|
||||
)
|
||||
|
||||
func CreateToken() (err error, token string) {
|
||||
|
@ -15,7 +18,7 @@ func CreateToken() (err error, token string) {
|
|||
log.Fatal(err)
|
||||
return
|
||||
}
|
||||
token = string(b)
|
||||
token = fmt.Sprintf("%x", b)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -26,7 +29,7 @@ func CreateUUID() (err error, uuid string) {
|
|||
log.Fatal(err)
|
||||
return
|
||||
}
|
||||
uuid = fmt.Sprintf("%x-%x-%x-%x-%x", b[0:4], b[4:6], b[6:8], b[8:10], b[10:])
|
||||
uuid = fmt.Sprintf("%x_%x_%x_%x_%x", b[0:4], b[4:6], b[6:8], b[8:10], b[10:])
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -40,3 +43,23 @@ func Hash(s []byte) (err error, hashString string) {
|
|||
hashString = base64.StdEncoding.EncodeToString(hash)
|
||||
return
|
||||
}
|
||||
|
||||
func GenerateKeyPair() (publicKey ed25519.PublicKey, privateKey ed25519.PrivateKey, err error) {
|
||||
publicKey, privateKey, err = ed25519.GenerateKey(nil)
|
||||
return
|
||||
}
|
||||
|
||||
func Sign(message []byte) []byte {
|
||||
return ed25519.Sign(config.PrivateKey, message)
|
||||
}
|
||||
|
||||
func SignContent(content []byte) (signatures map[string]map[string]string) {
|
||||
signatures = make(map[string]map[string]string)
|
||||
signatures[config.Homeserver] = make(map[string]string)
|
||||
signatures[config.Homeserver][config.KeyId] = string(Sign(content))
|
||||
return
|
||||
}
|
||||
|
||||
func VerifySignature(publicKey []byte, message []byte, signature []byte) bool {
|
||||
return ed25519.Verify(publicKey, message, signature)
|
||||
}
|
||||
|
|
|
@ -1,11 +1,24 @@
|
|||
package utils
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"nutfactory.org/Matrix/config"
|
||||
)
|
||||
|
||||
type RequestSummary struct {
|
||||
Method string `json:"method,omitempty"`
|
||||
Uri string `json:"uri,omitempty"`
|
||||
Origin string `json:"origin,omitempty"`
|
||||
Destination string `json:"destination,omitempty"`
|
||||
Content string `json:"content,omitempty"`
|
||||
Signatures map[string]map[string]string `json:"signatures,omitempty"`
|
||||
}
|
||||
|
||||
type ErrorResponse struct {
|
||||
ErrorCode string `json:"errcode,omitempty"`
|
||||
ErrorMessage string `json:"error,omitempty"`
|
||||
|
@ -19,6 +32,72 @@ func CheckRequest(r *http.Request) (response *ErrorResponse) {
|
|||
return
|
||||
}
|
||||
|
||||
func CheckAuthHeader(r *http.Request) (response *ErrorResponse) {
|
||||
authHeader := r.Header.Get("Authorization")
|
||||
if authHeader == "" || !strings.Contains(authHeader, "X-Matrix") {
|
||||
response = &ErrorResponse{ErrorMessage: "Missing Authorization Header"}
|
||||
return
|
||||
}
|
||||
keys := strings.Split(authHeader, ",")
|
||||
origin := strings.Split(keys[0], "=")[1]
|
||||
if !strings.Contains(keys[2], "ed25519") {
|
||||
response = &ErrorResponse{ErrorMessage: "Missing ed25519 Signature Key"}
|
||||
return
|
||||
}
|
||||
key := strings.Split(strings.Replace(strings.Split(keys[2], "=")[1], "\"", "", 2), ":")[1]
|
||||
signature := strings.Replace(strings.Split(keys[2], "=")[1], "\"", "", 2)
|
||||
buf := new(bytes.Buffer)
|
||||
buf.ReadFrom(r.Body)
|
||||
content := buf.String()
|
||||
requestSummary := RequestSummary{
|
||||
Method: r.Method,
|
||||
Uri: r.RequestURI,
|
||||
Origin: origin,
|
||||
Destination: config.Homeserver,
|
||||
Content: content,
|
||||
}
|
||||
requestSummaryString, err := json.Marshal(requestSummary)
|
||||
if err != nil {
|
||||
response = &ErrorResponse{ErrorMessage: "Error Creating Auth JSON String"}
|
||||
return
|
||||
}
|
||||
correct := VerifySignature([]byte(key), requestSummaryString, []byte(signature))
|
||||
if !correct {
|
||||
response = &ErrorResponse{ErrorMessage: "Signature in Auth Header is incorrect"}
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func CreateAuthHeader(method string, uri string, destination string, content string) (authHeader string, err error) {
|
||||
requestSummary := RequestSummary{
|
||||
Method: method,
|
||||
Uri: uri,
|
||||
Origin: config.Homeserver,
|
||||
Destination: destination,
|
||||
Content: content,
|
||||
}
|
||||
SigningContent, err := json.Marshal(requestSummary)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
authHeader = fmt.Sprintf("X-Matrix origin=%s,key=\"%s\",sig=\"%s\"", config.Homeserver, config.KeyId, Sign(SigningContent))
|
||||
return
|
||||
}
|
||||
|
||||
func GetAccessToken(r *http.Request) (token string, response *ErrorResponse) {
|
||||
token = r.URL.Query().Get("access_token")
|
||||
if token == "" {
|
||||
token = r.Header.Get("Authorization")
|
||||
if token == "" || !strings.Contains(token, "Bearer") {
|
||||
response = &ErrorResponse{ErrorCode: "M_MISSING_TOKEN"}
|
||||
} else {
|
||||
token = strings.Split(token, " ")[1]
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func IsJSONString(s string) bool {
|
||||
var js string
|
||||
return json.Unmarshal([]byte(s), &js) == nil
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue