Ejemplo n.º 1
0
func NewClient(ws *websocket.Conn, server *Server, name string) *Client {
	if ws == nil {
		panic("ws cannot be nil")
	}

	if server == nil {
		panic("server cannot be nil")
	}

	id, err := database.GetUserID(server.db, name)
	if err != nil {
		log.Println("Error:", err)
		return nil
	}

	ch := make(chan *database.Message, channelBufSize)
	doneCh := make(chan bool)

	return &Client{id, name, ws, server, ch, doneCh, ""}
}
Ejemplo n.º 2
0
func Login(db *sql.DB, username, password string) (*UserToken, error) {
	var t *UserToken
	dbPasswordHash, err := database.GetPasswordHash(db, username)
	if err != nil {
		if err == sql.ErrNoRows {
			return t, ErrUsernameNotFound
		}
		return t, err
	}

	err = bcrypt.CompareHashAndPassword(dbPasswordHash, []byte(password))
	if err != nil {
		if err == bcrypt.ErrMismatchedHashAndPassword {
			return t, ErrInvalidPassword
		}
		return t, err
	}

	t = NewUserToken()
	for selectorExists := true; selectorExists; {
		t.Random()
		selectorExists, err = database.CheckSelectorExists(db, t.Selector)
		if err != nil {
			return t, err
		}
	}

	dbToken := t.DBToken()
	userID, err := database.GetUserID(db, username)
	if err != nil {
		return t, err
	}
	dbToken.UserID = userID

	err = database.AddToken(db, dbToken)
	if err != nil {
		return t, err
	}

	return t, nil
}