server.go 2.9 KB
Newer Older
1
package server
2 3

import (
4 5 6
	"fmt"
	"os"

7 8
	"dfss/dfssp/api"
	"dfss/dfssp/authority"
Loïck Bonniot's avatar
Loïck Bonniot committed
9
	"dfss/dfssp/common"
10
	"dfss/dfssp/contract"
Loïck Bonniot's avatar
Loïck Bonniot committed
11
	"dfss/dfssp/user"
12
	"dfss/mgdb"
13
	"dfss/net"
14
	"golang.org/x/net/context"
15
	"google.golang.org/grpc"
16 17
)

18
type platformServer struct {
Loïck Bonniot's avatar
Loïck Bonniot committed
19 20
	Pid          *authority.PlatformID
	DB           *mgdb.MongoManager
Loïck Bonniot's avatar
Loïck Bonniot committed
21
	Rooms        *common.WaitingGroupMap
Loïck Bonniot's avatar
Loïck Bonniot committed
22 23
	CertDuration int
	Verbose      bool
24
}
25 26 27 28

// Register handler
//
// Handle incoming RegisterRequest messages
29
func (s *platformServer) Register(ctx context.Context, in *api.RegisterRequest) (*api.ErrorCode, error) {
Loïck Bonniot's avatar
Loïck Bonniot committed
30
	return user.Register(s.DB, in)
31 32 33 34 35
}

// Auth handler
//
// Handle incoming AuthRequest messages
36
func (s *platformServer) Auth(ctx context.Context, in *api.AuthRequest) (*api.RegisteredUser, error) {
Loïck Bonniot's avatar
Loïck Bonniot committed
37
	return user.Auth(s.Pid, s.DB, s.CertDuration, in)
38 39 40 41 42
}

// Unregister handler
//
// Handle incoming UnregisterRequest messages
43
func (s *platformServer) Unregister(ctx context.Context, in *api.Empty) (*api.ErrorCode, error) {
44 45 46 47 48 49 50
	// TODO
	return nil, nil
}

// PostContract handler
//
// Handle incoming PostContractRequest messages
51
func (s *platformServer) PostContract(ctx context.Context, in *api.PostContractRequest) (*api.ErrorCode, error) {
52 53 54 55 56 57

	cn := net.GetCN(&ctx)
	if len(cn) == 0 {
		return &api.ErrorCode{Code: api.ErrorCode_BADAUTH}, nil
	}

58 59
	builder := contract.NewContractBuilder(s.DB, in)
	return builder.Execute(), nil
60 61 62 63 64
}

// JoinSignature handler
//
// Handle incoming JoinSignatureRequest messages
65
func (s *platformServer) JoinSignature(in *api.JoinSignatureRequest, stream api.Platform_JoinSignatureServer) error {
Loïck Bonniot's avatar
Loïck Bonniot committed
66 67 68 69 70 71 72 73 74 75 76

	ctx := stream.Context()
	cn := net.GetCN(&ctx)
	if len(cn) == 0 {
		_ = stream.Send(&api.UserConnected{
			ErrorCode: &api.ErrorCode{Code: api.ErrorCode_BADAUTH},
		})
		return nil
	}

	contract.JoinSignature(s.DB, s.Rooms, in, stream)
77
	return nil
78 79 80 81 82
}

// ReadySign handler
//
// Handle incoming ReadySignRequest messages
83
func (s *platformServer) ReadySign(ctx context.Context, in *api.ReadySignRequest) (*api.LaunchSignature, error) {
Loïck Bonniot's avatar
Loïck Bonniot committed
84 85 86 87 88
	cn := net.GetCN(&ctx)
	if len(cn) == 0 {
		return &api.LaunchSignature{ErrorCode: &api.ErrorCode{Code: api.ErrorCode_BADAUTH}}, nil
	}
	return contract.ReadySign(s.DB, s.Rooms, &ctx, in), nil
89
}
90 91

// GetServer returns the GRPC server associated with the platform
Loïck Bonniot's avatar
Loïck Bonniot committed
92
func GetServer(keyPath, db string, certValidity int, verbose bool) *grpc.Server {
93 94 95 96 97 98 99 100 101 102 103 104 105 106
	pid, err := authority.Start(keyPath)
	if err != nil {
		fmt.Println("An error occured during the private key and root certificate retrieval:", err)
		os.Exit(1)
	}

	dbManager, err := mgdb.NewManager(db)
	if err != nil {
		fmt.Println("An error occured during the connection to MongoDB:", err)
		os.Exit(1)
	}

	server := net.NewServer(pid.RootCA, pid.Pkey, pid.RootCA)
	api.RegisterPlatformServer(server, &platformServer{
Loïck Bonniot's avatar
Loïck Bonniot committed
107 108
		Pid:          pid,
		DB:           dbManager,
Loïck Bonniot's avatar
Loïck Bonniot committed
109
		Rooms:        common.NewWaitingGroupMap(),
Loïck Bonniot's avatar
Loïck Bonniot committed
110 111
		CertDuration: certValidity,
		Verbose:      verbose,
112 113 114
	})
	return server
}