Commit be66c841 authored by Loïck Bonniot's avatar Loïck Bonniot

Merge branch '217_219_unregister_user' into 'master'

[p][c] Unregister user

- Add `unregister` command to the client
- Handle `UnregisterRequest` plateform-side

See merge request !85
parents 735ee96c 5585cc88
Pipeline #2287 passed with stages
......@@ -58,6 +58,6 @@ func init() {
_ = viper.BindPFlag("timeout", RootCmd.PersistentFlags().Lookup("timeout"))
// Bind subcommands to root
RootCmd.AddCommand(dfss.VersionCmd, registerCmd, authCmd, newCmd, showCmd, fetchCmd, importCmd, exportCmd, signCmd)
RootCmd.AddCommand(dfss.VersionCmd, registerCmd, authCmd, newCmd, showCmd, fetchCmd, importCmd, exportCmd, signCmd, unregisterCmd)
}
package cmd
import (
"fmt"
"os"
"dfss/dfssc/security"
"dfss/dfssc/user"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var unregisterCmd = &cobra.Command{
Use: "unregister",
Short: "delete current client information on platform",
Run: func(cmd *cobra.Command, args []string) {
// Read info from provided certificate
cert, err := security.GetCertificate(viper.GetString("file_cert"))
if err != nil {
fmt.Fprintln(os.Stderr, "An error occurred:", err.Error())
os.Exit(2)
}
// Confirmation
var ready string
readStringParam("Do you REALLY want to delete "+cert.Subject.CommonName+"? Type 'yes' to confirm", "", &ready)
if ready != "yes" {
fmt.Fprintln(os.Stderr, "Unregistering aborted!")
os.Exit(1)
}
err = user.Unregister()
if err != nil {
fmt.Fprintln(os.Stderr, "Cannot unregister:", err.Error())
os.Exit(2)
}
},
}
package user
import (
"errors"
"golang.org/x/net/context"
"google.golang.org/grpc"
pb "dfss/dfssp/api"
"dfss/net"
)
// Unregister a user from the platform
func Unregister() error {
client, err := connect()
if err != nil {
return err
}
// Stop the context if it takes too long for the platform to answer
ctx, cancel := context.WithTimeout(context.Background(), net.DefaultTimeout)
defer cancel()
response, err := client.Unregister(ctx, &pb.Empty{})
if err != nil {
return errors.New(grpc.ErrorDesc(err))
}
if response.Code != pb.ErrorCode_SUCCESS {
return errors.New(response.Message)
}
return nil
}
......@@ -44,8 +44,7 @@ func (s *platformServer) Auth(ctx context.Context, in *api.AuthRequest) (*api.Re
//
// Handle incoming UnregisterRequest messages
func (s *platformServer) Unregister(ctx context.Context, in *api.Empty) (*api.ErrorCode, error) {
// TODO
return nil, nil
return user.Unregister(s.DB, net.GetClientHash(&ctx)), nil
}
// PostContract handler
......
package user
import (
api "dfss/dfssp/api"
"dfss/mgdb"
"gopkg.in/mgo.v2/bson"
)
// Unregister delete a user based on the provided certificate hash
func Unregister(manager *mgdb.MongoManager, userCertificateHash []byte) *api.ErrorCode {
count, err := manager.Get("users").DeleteAll(bson.M{
"certHash": userCertificateHash,
})
if err != nil || count == 0 {
return &api.ErrorCode{Code: api.ErrorCode_INVARG, Message: "No user matching provided certificate"}
}
return &api.ErrorCode{Code: api.ErrorCode_SUCCESS}
}
......@@ -14,6 +14,7 @@ import (
"dfss/dfssp/api"
"dfss/dfssp/entities"
"dfss/dfssp/server"
u "dfss/dfssp/user"
"dfss/mgdb"
"dfss/net"
"github.com/spf13/viper"
......@@ -122,6 +123,25 @@ func TestMongoInsertUser(t *testing.T) {
}
}
func TestUnregisterUser(t *testing.T) {
var hash = []byte{0xde, 0xad, 0xbe, 0xef}
user := entities.NewUser()
user.Email = "dfss2@mpcs.tk"
user.CertHash = hash
user.Csr = "csr2"
user.RegToken = "regToken 2"
_, err = repository.Collection.Insert(user)
if err != nil {
t.Fatal("An error occurred while inserting the user")
}
response := u.Unregister(manager, hash)
if response.Code != api.ErrorCode_SUCCESS {
t.Fatal("An error occured while deleting the user:" + response.Message)
}
}
func equalUsers(t *testing.T, user1, user2 *entities.User) {
if user1.ID != user2.ID {
t.Fatal("ID doesn't match : received ", user1.ID, " and ", user2.ID)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment