Exemple #1
0
func getTokenFromHeader(w http.ResponseWriter, r *http.Request) (token string, err error) {
	// Get token from header
	token = r.Header.Get("X-Auth-Token")
	if token == "" {
		return "", errors.New("httpApiHandlers: Could not retrieve token from headers")
	}

	// Check token
	err = appauth.CheckToken(token)
	if err != nil {
		return "", errors.New("httpApiHandlers: Token invalid")
	}

	return
}
Exemple #2
0
func processCommand(text string) (result interface{}, err error) {
	// Commands are received split by tilde (~)
	// command~DATA
	cleanText := strings.Replace(text, "\n", "", -1)
	fmt.Printf("### %s ####\n", cleanText)
	command := strings.Split(cleanText, "~")

	// Check if we received a command
	if len(command) == 0 {
		fmt.Println("No command received")
		return
	}

	// Remove null termination from data
	command[len(command)-1] = string(bytes.Trim([]byte(command[len(command)-1]), "\x00"))

	// Check application auth. This is always the first value, if no token a 0 is sent
	isCreateAccount := (command[0] == "0" && command[1] == "acmt" && command[2] == "1")
	isLogIn := (command[0] == "0" && command[1] == "appauth" && command[2] == "2")
	isCreateUserPassword := (command[0] == "0" && command[1] == "appauth" && command[2] == "3")

	if !isCreateAccount || !isLogIn || !isCreateUserPassword {
		err := appauth.CheckToken(command[0])
		if err != nil {
			return "", errors.New("server.processCommand: " + err.Error())
		}
	}

	switch command[1] {
	case "appauth":
		// Check "help"
		if command[2] == "help" {
			return "Format of appauth: appauth~userName~password", nil
		}
		result, err = appauth.ProcessAppAuth(command)
		if err != nil {
			return "", errors.New("server.processCommand: " + err.Error())
		}
		break
	case "pain":
		// Check "help"
		if command[2] == "help" {
			return "Format of PAIN transaction:\npain\npainType~senderAccountNumber@SenderBankNumber\nreceiverAccountNumber@ReceiverBankNumber\ntransactionAmount\n\nBank numbers may be left void if bank is local", nil
		}
		result, err = transactions.ProcessPAIN(command)
		if err != nil {
			return "", errors.New("server.processCommand: " + err.Error())
		}
	case "camt":
	case "acmt":
		// Check "help"
		if command[2] == "help" {
			return "", nil // @TODO Help section
		}
		result, err = accounts.ProcessAccount(command)
	case "remt":
	case "reda":
	case "pacs":
	case "auth":
		break
	default:
		return "No valid command received", nil
	}

	return
}