예제 #1
0
func (a *Api) addUserAndSendStatus(user *User, res http.ResponseWriter, req *http.Request) {
	if err := a.Store.UpsertUser(user); err != nil {
		a.logger.Println(http.StatusInternalServerError, STATUS_ERR_CREATING_USR, err.Error())
		sendModelAsResWithStatus(res, status.NewStatus(http.StatusInternalServerError, STATUS_ERR_CREATING_USR), http.StatusInternalServerError)
		return
	}
	if sessionToken, err := CreateSessionTokenAndSave(
		&TokenData{DurationSecs: extractTokenDuration(req), UserId: user.Id, IsServer: false},
		TokenConfig{DurationSecs: a.ApiConfig.TokenDurationSecs, Secret: a.ApiConfig.Secret},
		a.Store); err != nil {
		a.logger.Println(http.StatusInternalServerError, STATUS_ERR_GENERATING_TOKEN, err.Error())
		sendModelAsResWithStatus(res, status.NewStatus(http.StatusInternalServerError, STATUS_ERR_GENERATING_TOKEN), http.StatusInternalServerError)
		return
	} else {
		a.logMetric("usercreated", sessionToken.Id, nil)
		res.Header().Set(TP_SESSION_TOKEN, sessionToken.Id)
		sendModelAsResWithStatus(res, user, http.StatusCreated)
		return
	}
}
예제 #2
0
func (client *UserClient) Login(username, password string) (*commonUserApi.UserData, string, error) {

	request, _ := http.NewRequest("POST", "", nil)
	request.SetBasicAuth(username, password)
	response := httptest.NewRecorder()

	client.userapi.Login(response, request)

	body, _ := ioutil.ReadAll(response.Body)

	switch response.Code {
	case 200:
		ud, err := extractUserData(string(body))
		if err != nil {
			return nil, "", err
		}

		return ud, response.Header().Get("x-tidepool-session-token"), nil
	case 404:
		return nil, "", nil
	default:
		return nil, "", &status.StatusError{status.NewStatus(response.Code, "Unknown response code from user api")}
	}
}
예제 #3
0
func (client *UserClient) Signup(username, password, email string) (*commonUserApi.UserData, error) {

	data := []byte(fmt.Sprintf(`{"username": "******", "password": "******","emails":["%s"]}`, username, password, email))
	request, _ := http.NewRequest("POST", "/user", bytes.NewBuffer(data))
	request.Header.Add("content-type", "application/json")

	response := httptest.NewRecorder()

	client.userapi.CreateUser(response, request)

	body, _ := ioutil.ReadAll(response.Body)

	switch response.Code {
	case http.StatusCreated:
		ud, err := extractUserData(string(body))
		if err != nil {
			return nil, err
		}

		return ud, nil
	default:
		return nil, &status.StatusError{status.NewStatus(response.Code, "There was an issue trying to signup a new user")}
	}
}