2024-01-12 19:19:52 +01:00
|
|
|
package pass
|
|
|
|
|
|
|
|
import (
|
2024-05-06 08:27:08 +02:00
|
|
|
"context"
|
2024-01-24 20:57:31 +01:00
|
|
|
"log"
|
|
|
|
|
2024-05-06 08:27:08 +02:00
|
|
|
firebase "firebase.google.com/go/v4"
|
|
|
|
"firebase.google.com/go/v4/messaging"
|
2024-01-24 20:57:31 +01:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/aws/session"
|
|
|
|
"github.com/aws/aws-sdk-go/service/kms"
|
2024-01-12 19:19:52 +01:00
|
|
|
"github.com/gin-gonic/gin"
|
2024-05-06 08:27:08 +02:00
|
|
|
"google.golang.org/api/option"
|
2024-01-24 20:57:31 +01:00
|
|
|
|
|
|
|
"github.com/twofas/2fas-server/config"
|
2024-01-12 19:19:52 +01:00
|
|
|
httphelpers "github.com/twofas/2fas-server/internal/common/http"
|
|
|
|
"github.com/twofas/2fas-server/internal/common/recovery"
|
2024-04-05 11:31:40 +02:00
|
|
|
"github.com/twofas/2fas-server/internal/pass/connection"
|
2024-01-12 19:19:52 +01:00
|
|
|
"github.com/twofas/2fas-server/internal/pass/pairing"
|
2024-04-05 11:31:40 +02:00
|
|
|
"github.com/twofas/2fas-server/internal/pass/sign"
|
|
|
|
"github.com/twofas/2fas-server/internal/pass/sync"
|
2024-01-12 19:19:52 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type Server struct {
|
|
|
|
router *gin.Engine
|
|
|
|
addr string
|
|
|
|
}
|
|
|
|
|
2024-01-24 20:57:31 +01:00
|
|
|
func NewServer(cfg config.PassConfig) *Server {
|
|
|
|
var awsEndpoint *string
|
|
|
|
if cfg.AWSEndpoint != "" {
|
|
|
|
awsEndpoint = aws.String(cfg.AWSEndpoint)
|
|
|
|
}
|
2024-01-24 21:05:16 +01:00
|
|
|
region := cfg.AWSRegion
|
|
|
|
if region == "" {
|
|
|
|
region = "us-east-1"
|
|
|
|
}
|
2024-01-25 17:55:08 +01:00
|
|
|
sess, err := session.NewSessionWithOptions(
|
|
|
|
session.Options{
|
|
|
|
Config: aws.Config{
|
|
|
|
Region: aws.String(region),
|
|
|
|
S3ForcePathStyle: aws.Bool(true),
|
|
|
|
Endpoint: awsEndpoint,
|
|
|
|
},
|
|
|
|
SharedConfigState: session.SharedConfigEnable,
|
|
|
|
})
|
2024-01-24 20:57:31 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
kmsClient := kms.New(sess)
|
|
|
|
|
|
|
|
signSvc, err := sign.NewService(cfg.KMSKeyID, kmsClient)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2024-05-06 08:27:08 +02:00
|
|
|
ctx := context.Background()
|
|
|
|
var fcmClient *messaging.Client
|
|
|
|
if cfg.FirebaseServiceAccount != "" {
|
|
|
|
opt := option.WithCredentialsJSON([]byte(cfg.FirebaseServiceAccount))
|
|
|
|
app, err := firebase.NewApp(ctx, nil, opt)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error initializing FCM App: %v", err)
|
|
|
|
}
|
|
|
|
fcmClient, err = app.Messaging(ctx)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error initializing Messaging Client: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// TODO: use client in later phase.
|
|
|
|
_ = fcmClient
|
|
|
|
|
2024-04-05 11:31:40 +02:00
|
|
|
pairingApp := pairing.NewApp(signSvc, cfg.PairingRequestTokenValidityDuration)
|
|
|
|
proxyPairingApp := connection.NewProxyServer("device_id")
|
|
|
|
|
|
|
|
syncApp := sync.NewApp(signSvc, cfg.FakeMobilePush)
|
|
|
|
proxySyncApp := connection.NewProxyServer("fcm_token")
|
2024-01-12 19:19:52 +01:00
|
|
|
|
|
|
|
router := gin.New()
|
|
|
|
router.Use(recovery.RecoveryMiddleware())
|
2024-04-27 15:24:58 +02:00
|
|
|
router.Use(httphelpers.LoggingMiddleware())
|
2024-01-12 19:19:52 +01:00
|
|
|
router.Use(httphelpers.RequestJsonLogger())
|
|
|
|
|
|
|
|
router.GET("/health", func(context *gin.Context) {
|
|
|
|
context.Status(200)
|
|
|
|
})
|
|
|
|
|
2024-04-05 11:31:40 +02:00
|
|
|
// Deprecated paths start here.
|
2024-01-21 10:25:12 +01:00
|
|
|
router.GET("/browser_extension/wait_for_connection", pairing.ExtensionWaitForConnWSHandler(pairingApp))
|
2024-04-05 11:31:40 +02:00
|
|
|
router.GET("/browser_extension/proxy_to_mobile", pairing.ExtensionProxyWSHandler(pairingApp, proxyPairingApp))
|
2024-01-12 19:19:52 +01:00
|
|
|
router.POST("/mobile/confirm", pairing.MobileConfirmHandler(pairingApp))
|
2024-04-05 11:31:40 +02:00
|
|
|
router.GET("/mobile/proxy_to_browser_extension", pairing.MobileProxyWSHandler(pairingApp, proxyPairingApp))
|
|
|
|
// Deprecated paths end here.
|
|
|
|
|
|
|
|
router.POST("/browser_extension/configure", pairing.ExtensionConfigureHandler(pairingApp))
|
|
|
|
|
|
|
|
router.GET("/browser_extension/pairing/wait", pairing.ExtensionWaitForConnWSHandler(pairingApp))
|
|
|
|
router.GET("/browser_extension/pairing/proxy", pairing.ExtensionProxyWSHandler(pairingApp, proxyPairingApp))
|
|
|
|
router.POST("/mobile/pairing/confirm", pairing.MobileConfirmHandler(pairingApp))
|
|
|
|
router.GET("/mobile/pairing/proxy", pairing.MobileProxyWSHandler(pairingApp, proxyPairingApp))
|
|
|
|
|
2024-05-07 14:04:44 +02:00
|
|
|
router.POST("/browser_extension/sync/request", sync.ExtensionRequestSync(syncApp))
|
|
|
|
router.POST("/browser_extension/sync/push", sync.ExtensionRequestPush(syncApp))
|
|
|
|
router.GET("/browser_extension/sync/wait", sync.ExtensionRequestWait(syncApp))
|
|
|
|
|
2024-04-05 11:31:40 +02:00
|
|
|
router.GET("/browser_extension/sync/proxy", sync.ExtensionProxyWSHandler(syncApp, proxySyncApp))
|
|
|
|
router.POST("/mobile/sync/confirm", sync.MobileConfirmHandler(syncApp))
|
|
|
|
router.GET("/mobile/sync/proxy", sync.MobileProxyWSHandler(syncApp, proxySyncApp))
|
|
|
|
|
2024-01-12 19:19:52 +01:00
|
|
|
return &Server{
|
|
|
|
router: router,
|
2024-01-24 20:57:31 +01:00
|
|
|
addr: cfg.Addr,
|
2024-01-12 19:19:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) Run() error {
|
|
|
|
return s.router.Run(s.addr)
|
|
|
|
}
|