예제 #1
0
func authenticate(token string) (*auth.User, error) {
	funcPrefix := fmt.Sprintf("Token '%s' authentication", token)
	log.Debug.Printf("%s: start\n", funcPrefix)
	defer log.Debug.Printf("%s: end\n", funcPrefix)
	if token == "123123" {
		u := &auth.User{
			Id:     "debug",
			Email:  "*****@*****.**",
			Device: 2,
			DevId:  "",
		}
		log.Debug.Printf("%s returns user [%+v]\n", funcPrefix, u)
		return u, nil
	}
	at, err := storage.LoadAuthToken(token)
	if err != nil {
		log.Error.Printf("%s returns error: %s\n", funcPrefix, err.Error())
		return nil, err
	}
	info, err := at.GetTokenInfo(secret)
	if err != nil {
		log.Error.Printf("%s returns error: %s\n", funcPrefix, err.Error())
		return nil, err
	}
	user, err := storage.LoadUser("user:"******"%s returns error: %s\n", funcPrefix, err.Error())
		return nil, err
	} else {
		log.Debug.Printf("%s returns user [%+v]\n", funcPrefix, user)
		return user, nil
	}
}
예제 #2
0
func authenticate(token string) (*auth.User, error) {
	if token == "123123" {
		u := &auth.User{
			Id:     "debug",
			Email:  "*****@*****.**",
			Device: 2,
			DevId:  "",
		}
		return u, nil
	}
	at, err := storage.LoadAuthToken(token)
	if err != nil {
		return nil, err
	}
	info, err := at.GetTokenInfo(secret)
	if err != nil {
		return nil, err
	}
	user, err := storage.LoadUser("user:" + info.Id)
	if err != nil {
		return nil, err
	} else {
		return user, nil
	}
}
예제 #3
0
func registerUser(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
	funcPrefix := "Registering new user"
	log.Debug.Printf("%s: start\n", funcPrefix)
	defer log.Debug.Printf("%s: end\n", funcPrefix)
	log.Debug.Printf("%s: getting params from request...\n", funcPrefix)
	id := r.PostFormValue("id")
	email := r.PostFormValue("email")
	firstname := r.PostFormValue("first_name")
	lastname := r.PostFormValue("last_name")
	device, _ := strconv.Atoi(r.PostFormValue("device"))
	dev_id := r.PostFormValue("dev_id")
	token := r.PostFormValue("token")

	if token != "BE7C411D475AEA4CF1D7B472D5BD1" {
		log.Warning.Printf("%s: token is not correct!\n", funcPrefix)
		w.WriteHeader(403)
		return
	}

	log.Debug.Printf("%s: checking existence of user with id '%s'...\n", funcPrefix, id)
	user, err := storage.LoadUser("user:"******"%s: user with id '%s' already exists; modifying his params...\n", funcPrefix, id)
		user.Device = device
		user.DevId = dev_id
		user.Email = email
		user.FirstName = firstname
		user.LastName = lastname
	} else {
		log.Debug.Printf("%s: user with id '%s' was not found; creating new user...\n", funcPrefix, id)
		user = &auth.User{
			Id:        id,
			Email:     email,
			Device:    device,
			DevId:     dev_id,
			FirstName: firstname,
			LastName:  lastname,
		}
	}
	log.Debug.Printf("%s: composing new auth token for user with id '%s'...\n", funcPrefix, id)
	at := auth.NewAuthToken(*user, time.Now(), secret)

	log.Debug.Printf("%s: saving user with id '%s' to storage...\n", funcPrefix, id)
	storage.SaveUser(*user)

	storage.SaveAuthToken(*at)

	log.Info.Printf("%s: user has been succesfully registered: [%+v]\n", funcPrefix, user)

	w.Header().Set("Content-Type", "application/json; charset=utf-8")
	w.Header().Set("Access-Control-Allow-Origin", "*")
	w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
	w.Header().Set("Access-Control-Allow-Headers", "*")
	rec := RegisterStatus{
		Token: at.HMAC,
	}
	if json.NewEncoder(w).Encode(rec) != nil {
		log.Error.Printf("%s: encoding response failed\n", funcPrefix)
		w.WriteHeader(500)
	}
}