fg-nucleus-library/main.go

87 lines
2.1 KiB
Go
Raw Normal View History

2024-10-04 17:30:02 +01:00
package library
2024-10-16 18:04:11 +01:00
import (
2025-01-08 14:18:25 +00:00
"crypto/ed25519"
2025-01-08 18:16:12 +00:00
"fmt"
2025-01-08 14:18:25 +00:00
library "git.ailur.dev/ailur/fg-library/v3"
2024-11-03 16:52:46 +00:00
"github.com/google/uuid"
2025-01-08 14:18:25 +00:00
"time"
2024-10-16 18:04:11 +01:00
)
2024-10-04 17:30:02 +01:00
type OAuthInformation struct {
Token string `json:"token"`
Name string `json:"name"`
RedirectUri string `json:"redirectUri"`
KeyShareUri string `json:"keyShareUri"`
Scopes []string `json:"scopes"`
}
type OAuthResponse struct {
AppID string `json:"appId"`
SecretKey string `json:"secretKey"`
}
2024-10-16 17:59:52 +01:00
type File struct {
Name string `validate:"required"`
User uuid.UUID `validate:"required"`
Bytes []byte // Only used in write operations
}
2024-11-03 16:52:46 +00:00
type Quota struct {
User uuid.UUID `validate:"required"`
Bytes int64 `validate:"required"`
}
2025-01-08 14:18:25 +00:00
var (
StorageService = uuid.MustParse("00000000-0000-0000-0000-000000000003")
OAuthService = uuid.MustParse("00000000-0000-0000-0000-000000000004")
)
2025-01-08 18:16:12 +00:00
func OAuthSignup(oauth OAuthInformation, information *library.ServiceInitializationInformation) (OAuthResponse, error) {
2025-01-08 14:18:25 +00:00
message, err := information.SendAndAwaitISMessage(OAuthService, 1, oauth, time.Second*3)
if err != nil {
return OAuthResponse{}, err
}
return message.Message.(OAuthResponse), nil
}
2025-01-08 18:16:12 +00:00
func GetPublicKey(information *library.ServiceInitializationInformation) (ed25519.PublicKey, error) {
2025-01-08 14:18:25 +00:00
message, err := information.SendAndAwaitISMessage(OAuthService, 2, nil, time.Second*3)
if err != nil {
return nil, err
}
return message.Message.(ed25519.PublicKey), nil
}
2025-01-08 18:16:12 +00:00
func GetOAuthHostname(information *library.ServiceInitializationInformation) (string, error) {
2025-01-08 14:18:25 +00:00
message, err := information.SendAndAwaitISMessage(OAuthService, 0, nil, time.Second*3)
if err != nil {
return "", err
}
return message.Message.(string), nil
}
2025-01-08 18:16:12 +00:00
func InitializeOAuth(oauth OAuthInformation, information *library.ServiceInitializationInformation) (oauthResponse OAuthResponse, pk ed25519.PublicKey, hostname string, err error) {
fmt.Println("Initializing OAuth")
2025-01-08 14:18:25 +00:00
pk, err = GetPublicKey(information)
if err != nil {
return
}
2025-01-08 18:16:12 +00:00
2025-01-08 14:18:25 +00:00
hostname, err = GetOAuthHostname(information)
if err != nil {
return
}
oauthResponse, err = OAuthSignup(oauth, information)
if err != nil {
return
}
return
}