Beispiel #1
0
// handler for: login
func LoginHandler(cmd bytengine.Command, user *bytengine.User, eng *bytengine.Engine) (interface{}, error) {
	usr := cmd.Args["username"].(string)
	pw := cmd.Args["password"].(string)
	duration := cmd.Args["duration"].(int64)

	ok := eng.Authentication.Authenticate(usr, pw)
	if !ok {
		err := fmt.Errorf("Authentication failed")
		return nil, err
	}

	key := auth.GenerateRandomKey(KeyStrength)
	if len(key) == 0 {
		err := fmt.Errorf("Token creation failed")
		return nil, err
	}

	token := fmt.Sprintf("%x", key)
	err := eng.StateStore.TokenSet(token, usr, 60*duration)
	if err != nil {
		err := fmt.Errorf("Token persistence failed: %s", err)
		return nil, err
	}

	return token, nil
}
Beispiel #2
0
// handler for: upload ticket
func UploadTicketHandler(cmd bytengine.Command, user *bytengine.User, eng *bytengine.Engine) (interface{}, error) {
	// check if user is anonymous
	if user == nil {
		err := fmt.Errorf("Authorization required")
		return nil, err
	}

	db := cmd.Database
	path := cmd.Args["path"].(string)
	duration := cmd.Args["duration"].(int64)
	// check if path exists
	r, err := eng.FileSystem.Info(path, db)
	if err != nil {
		return r, err
	}
	// create ticket
	key := auth.GenerateRandomKey(KeyStrength)
	if len(key) == 0 {
		err := fmt.Errorf("Ticket creation failed")
		return nil, err
	}

	ticket := fmt.Sprintf("%x", key)
	val := map[string]string{
		"database": db,
		"path":     path,
	}
	b, err := json.Marshal(val)
	if err != nil {
		err := fmt.Errorf("Ticket creation failed")
		return nil, err
	}
	err = eng.StateStore.CacheSet(ticket, string(b), 60*duration)
	if err != nil {
		err := fmt.Errorf("Ticket creation failed")
		return nil, err
	}

	return ticket, nil
}