コード例 #1
0
func (uc *Userclient) iCreateUser(args *userproto.CreateUserArgs, reply *userproto.CreateUserReply) error {
	if uc.user.Username != "" {
		//User must log out first before creating another user
		reply.Status = userproto.EEXISTS
		return nil
	}
	//check to see if Username already exists
	getUser, _ := uc.midclient.Get(args.Username, "")
	//if it doesn't we are good to go
	if getUser == "" {
		//make new user
		//LATER: should actually hash the password before we store it.....
		user := &userproto.User{Username: args.Username, Password: args.Password, Email: args.Email, Classes: []string{}}
		//marshal it
		userjson, marshalErr := json.Marshal(user)
		if marshalErr != nil {
			return marshalErr
		}
		//send it to the server
		createErr := uc.midclient.Put(args.Username, string(userjson), "")
		if createErr != nil {
			return createErr
		}
		//set the current user of the session to the one we just created
		uc.user = user

		dirErr := os.MkdirAll(uc.homedir, os.ModeDir)
		if dirErr != nil {
			return dirErr
		}

		watcher, watchErr := fsnotify.NewWatcher()
		if watchErr != nil {
			log.Fatal(watchErr)
		}
		uc.watcher = watcher
		go uc.iMonitorLocalChanges()
		watchErr = watcher.Watch(uc.homedir)

		if watchErr != nil {
			log.Fatal(watchErr)
		}

		cdErr := os.Chdir(uc.homedir)
		if cdErr != nil {
			return cdErr
		}

		//Set up hidden file with permissions and file/key cache
		permFile, permErr := os.Create(".permkey")

		permFile.Chmod(os.FileMode(444))
		if permErr != nil {
			return permErr
		}

		permKeyMutex := make(chan int, 1)
		permKeyMutex <- 1
		uc.permkeyFileMutex = permKeyMutex
		uc.permkeyFile = permFile

		reply.Status = userproto.OK
		return nil
	}
	//otherwise the name already exists and we must chose another name
	reply.Status = userproto.EEXISTS
	return nil
}
コード例 #2
0
func (uc *Userclient) iAuthenticateUser(args *userproto.AuthenticateUserArgs, reply *userproto.AuthenticateUserReply) error {
	userJSON, exists := uc.midclient.Get(args.Username, "")
	if exists == nil {
		var user *userproto.User
		jsonBytes := []byte(userJSON)
		//unmarshall the data
		unmarshalErr := json.Unmarshal(jsonBytes, &user)
		if unmarshalErr != nil {
			return unmarshalErr
		}
		//check if the passwords match
		//LATER: should actually just hash the password and then check the hashes
		if args.Password == user.Password {
			//if they do it's all good and we are logged in
			reply.Status = userproto.OK
			//Get user data for temporary session
			uc.user = user
			file, readErr := os.Open(uc.homedir)
			if readErr != nil {
				dirErr := os.MkdirAll(uc.homedir, os.ModeDir)
				if dirErr != nil {
					return dirErr
				}
			} else {
				file.Close()
			}

		} else {
			reply.Status = userproto.WRONGPASSWORD
			return nil
		}
		permMutex := make(chan int, 1)
		permMutex <- 1
		uc.permkeyFileMutex = permMutex

		permkeyFile, permkeyErr := os.Open(uc.homedir + "/.permkey")
		uc.permkeyFile = permkeyFile

		//if permkey file exists...
		//construct table
		if permkeyErr == nil {
			fmt.Println("Exists")
			uc.iConstructFileKeyMap()
		} else { //else make it!
			fmt.Println("Make permkey")
			permFile, permErr := os.Create(".permkey")
			permFile.Chmod(os.FileMode(444))
			if permErr != nil {
				return permErr
			}
		}

		watcher, watchErr := fsnotify.NewWatcher()
		if watchErr != nil {
			log.Fatal(watchErr)
		}
		uc.watcher = watcher
		go uc.iMonitorLocalChanges()

		watchErr = watcher.Watch(uc.homedir)

		if watchErr != nil {
			log.Fatal(watchErr)
		}

		cdErr := os.Chdir(uc.homedir)
		if cdErr != nil {
			return cdErr
		}

		go uc.iInitialFileCheck() // updates outdated file on local on login

		return nil
	}
	//if there isn't a user by that name just return error
	reply.Status = userproto.ENOSUCHUSER
	return nil
}