示例#1
0
// Delegate processes a delegation request.
func Delegate(jsonIn []byte) ([]byte, error) {
	var s delegate
	if err := json.Unmarshal(jsonIn, &s); err != nil {
		return jsonStatusError(err)
	}

	if passvault.NumRecords() == 0 {
		return jsonStatusError(errors.New("Vault is not created yet"))
	}

	// Find password record for user and verify that their password
	// matches. If not found then add a new entry for this user.

	pr, found := passvault.GetRecord(s.Name)
	if found {
		if err := pr.ValidatePassword(s.Password); err != nil {
			return jsonStatusError(err)
		}
	} else {
		var err error
		if pr, err = passvault.AddNewRecord(s.Name, s.Password, false); err != nil {
			log.Printf("Error adding record for %s: %s\n", s.Name, err)
			return jsonStatusError(err)
		}
	}

	// add signed-in record to active set
	if err := keycache.AddKeyFromRecord(pr, s.Name, s.Password, s.Uses, s.Time); err != nil {
		log.Printf("Error adding key to cache for %s: %s\n", s.Name, err)
		return jsonStatusError(err)
	}

	return jsonStatusOk()
}
示例#2
0
// Create processes a create request.
func Create(jsonIn []byte) ([]byte, error) {
	var s create
	if err := json.Unmarshal(jsonIn, &s); err != nil {
		return jsonStatusError(err)
	}

	if passvault.NumRecords() != 0 {
		return jsonStatusError(errors.New("Vault is already created"))
	}

	if _, err := passvault.AddNewRecord(s.Name, s.Password, true); err != nil {
		log.Printf("Error adding record for %s: %s\n", s.Name, err)
		return jsonStatusError(err)
	}

	return jsonStatusOk()
}