Beispiel #1
0
// asks for an usergrid access token with passed username and password
func generateAccessToken(request *falcore.Request, username string, password string) *http.Response {
	var response *http.Response

	uri := fmt.Sprintf("http://localhost:8080/newspeak/newspeak/token?grant_type=password&username=%s&password=%s", username, password)
	usergridResponse, usergridError := http.Get(uri)

	if usergridError != nil {
		request.CurrentStage.Status = byte(7)
		response = response_messages.InternalServerErrorResponse(request, "Could not request credentials", usergridError)
	} else if usergridResponse.StatusCode != http.StatusOK {
		request.CurrentStage.Status = byte(8)
		response = response_messages.ErrorResponse(request, http.StatusUnauthorized, "unauthorized", "Invalid credentials", nil)
		fmt.Println("invalid credentials: " + username + ", " + password)
	} else {
		usergridResponseBody, usergridError := ioutil.ReadAll(usergridResponse.Body)
		usergridResponse.Body.Close()

		if usergridError != nil {
			request.CurrentStage.Status = byte(9)
			response = response_messages.InternalServerErrorResponse(request, "Could not read response when requesting credentials", usergridError)
		} else {
			request.CurrentStage.Status = byte(10)
			response = parseUsergridResponse(request, username, usergridResponseBody)
		}
	}
	return response
}
Beispiel #2
0
// extract access token out of usergrid response and pass it on
func parseUsergridResponse(request *falcore.Request, username string, usergridResponseBody []byte) *http.Response {
	var response *http.Response

	var parsedJson interface{}
	unmarshalError := json.Unmarshal(usergridResponseBody, &parsedJson)
	if unmarshalError != nil {
		request.CurrentStage.Status = byte(11)
		response = response_messages.InternalServerErrorResponse(request, "Could not process response when requesting credentials", unmarshalError)
	} else {
		// extract access token and store in memcached
		var accessToken string
		parsedJsonMap := parsedJson.(map[string]interface{})
		for key, value := range parsedJsonMap {
			switch realValue := value.(type) {
			case string:
				if key == "access_token" {
					accessToken = realValue
					break
				}
			}
		}

		// store token request time and username in memcached
		time := time.Now().UnixNano()
		value := fmt.Sprintf("%s %s", strconv.FormatInt(time, 10), username)
		services.Memcached.Set(&memcache.Item{Key: accessToken, Value: []byte(value)})

		request.CurrentStage.Status = byte(12)
		response = accessTokenResponse(request, accessToken, username)
	}
	return response
}
Beispiel #3
0
// register device with apple/google push message server
// @TODO check if message is empty
func sendMessageToUniqush(request *falcore.Request) *http.Response {
	var response *http.Response
	recipient := request.HttpRequest.FormValue("recipient")
	message := request.HttpRequest.FormValue("message")

	fmt.Println(time.Now().Format("2006-01-02 15:04:05") + " delivering message '" + message + "' to '" + recipient + "'")

	uniqushResponse, uniqushError := http.PostForm("http://localhost:9898/push", url.Values{
		"service":    {"newspeak"},
		"subscriber": {recipient},
		"msg":        {message},
	})

	if uniqushError != nil {
		request.CurrentStage.Status = byte(2)
		response = response_messages.InternalServerErrorResponse(request, "Could not read response when registering device", uniqushError)
	} else {
		uniqushResponseBody, uniqushError := ioutil.ReadAll(uniqushResponse.Body)
		uniqushResponse.Body.Close()

		if uniqushError != nil {
			request.CurrentStage.Status = byte(3)
			response = response_messages.InternalServerErrorResponse(request, "Could not process response when registering device", uniqushError)
		} else if uniqushResponse.StatusCode != http.StatusOK {
			request.CurrentStage.Status = byte(4)
			response = response_messages.ErrorResponse(request, http.StatusServiceUnavailable, "service unavailable", "Error while connecting to push message server", errors.New(string(uniqushResponseBody)))
		} else {
			var body = make(map[string]string)
			body["Message"] = "message sent successfully"
			body["Recipient"] = recipient
			body["MessageSent"] = message
			body["Response"] = string(uniqushResponseBody)
			request.CurrentStage.Status = byte(5)
			fmt.Println(time.Now().Format("2006-01-02 15:04:05")+" delivered message '"+message+"' to '", recipient+"' response: "+string(uniqushResponseBody))
			response = response_messages.SuccessResponse(request, body)
		}
	}
	return response
}
Beispiel #4
0
// register device with apple/google push message server
func addDeviceToUniqush(request *falcore.Request, deviceToken string) *http.Response {
	var response *http.Response
	username := services.MemcacheGetUsername(getAccessToken(request))

	uniqushResponse, uniqushError := http.PostForm("http://localhost:9898/subscribe", url.Values{
		"service":         {"newspeak"},
		"pushservicetype": {"apns"},
		"subscriber":      {username},
		"devtoken":        {deviceToken},
	})

	if uniqushError != nil {
		request.CurrentStage.Status = byte(1)
		response = response_messages.InternalServerErrorResponse(request, "Could not read response when registering device", uniqushError)
	} else {
		uniqushResponseBody, uniqushError := ioutil.ReadAll(uniqushResponse.Body)
		uniqushResponse.Body.Close()

		if uniqushResponse.StatusCode != http.StatusOK {
			request.CurrentStage.Status = byte(2)
			response = response_messages.ErrorResponse(request, http.StatusServiceUnavailable, "service unavailable", "Error while connecting to push message server", errors.New(string(uniqushResponseBody)))
		} else if uniqushError != nil {
			request.CurrentStage.Status = byte(3)
			response = response_messages.InternalServerErrorResponse(request, "Could not process response when registering device", uniqushError)
		} else {
			var body = make(map[string]string)
			body["Message"] = "device added"
			body["Username"] = username
			body["DeviceToken"] = deviceToken
			body["Response"] = string(uniqushResponseBody)
			request.CurrentStage.Status = byte(4)
			fmt.Println("registered device:", deviceToken, "for:", username)
			response = response_messages.SuccessResponse(request, body)
		}
	}
	return response
}