Ejemplo n.º 1
0
func SyncTaskCreate(w http.ResponseWriter, r *http.Request) {
	//TODO. Check if there already is a task with either of the keys
	syncTask := sync.CreateSyncTask("", "", -1, Environment)
	body, err := ioutil.ReadAll(io.LimitReader(r.Body, 1048576))
	if err != nil {
		panic(err)
	}
	if err := r.Body.Close(); err != nil {
		panic(err)
	}
	if err := json.Unmarshal(body, &syncTask); err != nil {
		w.Header().Set("Content-Type", "application/json; charset=UTF-8")
		w.WriteHeader(422) // unprocessable entity
		if err := json.NewEncoder(w).Encode(err); err != nil {
			panic(err)
		}
	}

	//Creation set the last know timestamp to 1 hour ago
	syncTask.LastSeenTimestamp = nowMinusOneHourInUnix()

	db := sync.CreateSyncDbRepo(DbConnectionString)
	_, _, st, _ := db.StoreSyncTask(*syncTask)

	w.Header().Set("Content-Type", "application/json; charset=UTF-8")
	w.WriteHeader(http.StatusCreated)
	if err := json.NewEncoder(w).Encode(st); err != nil {
		panic(err)
	}
}
Ejemplo n.º 2
0
func oAuthSuccess(auth *strava.AuthorizationResponse, w http.ResponseWriter, r *http.Request) {
	db := sync.CreateSyncDbRepo(DbConnectionString)
	task, err := db.FindSyncTaskByToken(auth.AccessToken)
	if err != nil {
		log.Printf("Error loading token %s from database, aborting...", auth.AccessToken)
		w.WriteHeader(http.StatusInternalServerError)
	}

	runkeeperToken := auth.State
	if task == nil && (runkeeperToken == "" || runkeeperToken == "undefined") {
		syncTask := sync.CreateSyncTask("", "", -1, Environment)
		syncTask.StravaToken = auth.AccessToken
		syncTask.LastSeenTimestamp = nowMinusOneHourInUnix()
		_, _, _, err := db.StoreSyncTask(*syncTask)
		if err == nil {
			cookie := &http.Cookie{Name: "strava", Value: fmt.Sprintf("%s", syncTask.StravaToken), Expires: time.Now().Add(356 * 24 * time.Hour), HttpOnly: false}
			cookie.Domain = "www.syncmysport.com"
			http.SetCookie(w, cookie)
		} else {
			log.Printf("Error while creating a new SyncTask: %s, err: %s", syncTask, err)
		}

	} else {

		if task == nil {
			//find the task for runkeeper
			task, err = db.FindSyncTaskByToken(runkeeperToken)
			if err != nil {
				log.Printf("Error retrieving the RK based Task on Strava Auth")
			}
			log.Printf("Found: %s for Runkeeper SyncTask.", task)
			task.StravaToken = auth.AccessToken
		}

		//update cookie
		cookie := &http.Cookie{Name: "strava", Value: fmt.Sprintf("%s", task.StravaToken), Expires: time.Now().Add(356 * 24 * time.Hour), HttpOnly: false}
		cookie.Domain = "www.syncmysport.com"
		http.SetCookie(w, cookie)

		//if runkeeper is set, set that cookie to
		if task.RunkeeperToken != "" {
			cookie = &http.Cookie{Name: "runkeeper", Value: fmt.Sprintf("%s", task.RunkeeperToken), Expires: time.Now().Add(356 * 24 * time.Hour), HttpOnly: false}
			cookie.Domain = "www.syncmysport.com"
			http.SetCookie(w, cookie)
		}

		task.StravaToken = auth.AccessToken
		var i int
		i, err = db.UpdateSyncTask(*task)
		if i != 1 || err != nil {
			log.Printf("Error while updating synctask %s with token %s", task, auth.AccessToken)
		}
	}
	//redirect back to connect
	http.Redirect(w, r, "http://www.syncmysport.com/connect.html", 303) //replace by env var
}
Ejemplo n.º 3
0
func ObtainBearerToken(code string, stvToken string) (*sync.SyncTask, error) {
	tokenUrl := "https://runkeeper.com/apps/token"
	formData := make(map[string][]string)
	formData["grant_type"] = []string{"authorization_code"}
	formData["code"] = []string{code}
	formData["client_id"] = []string{RkClientId}
	formData["client_secret"] = []string{RkSecret}
	formData["redirect_uri"] = []string{RedirectUriRk}
	client := new(http.Client)
	response, err := client.PostForm(tokenUrl, formData)
	responseJson := make(map[string]string)
	if err == nil {
		responseBody, _ := ioutil.ReadAll(response.Body)
		json.Unmarshal(responseBody, &responseJson)
		token := responseJson["access_token"]
		db := sync.CreateSyncDbRepo(DbConnectionString)

		var task *sync.SyncTask
		if stvToken != "" && stvToken != "undefined" {
			task, err = db.FindSyncTaskByToken(stvToken)
		} else {
			task, err = db.FindSyncTaskByToken(token)
		}
		if task == nil || err != nil {
			syncTask := sync.CreateSyncTask("", "", -1, Environment)
			syncTask.RunkeeperToken = token
			syncTask.LastSeenTimestamp = nowMinusOneHourInUnix()
			db.StoreSyncTask(*syncTask)
			return syncTask, nil
		} else { //existing task
			if task.RunkeeperToken != token {
				task.RunkeeperToken = token
				db.UpdateSyncTask(*task)
			} else {
				log.Printf("Token %s is already stored for task id: %d", token, task.Uid)
			}
			return task, nil
		}
	} else {
		fmt.Print(err)
	}
	return nil, errors.New("Not happened")
}