// NewXHRSession returns a new XHRSession, a SockJS client which supports xhr-polling:
//
//   http://sockjs.github.io/sockjs-protocol/sockjs-protocol-0.3.3.html#section-74
//
// Deprecated: Use DialXHR instead.
175652
func NewXHRSession(opts *DialOptions) (*XHRSession, error) {
	cfg := config.New()
	cfg.XHR = opts.client()

	return DialXHR(opts.BaseURL, cfg)
}
1245010
func (c *Client) NewConversation() *ClientConversation {
	c.RLock()
	defer c.RUnlock()
	return &ClientConversation{
		client:   c,
		nonceGen: c.nonceGen,
		hashGen:  c.hashGen,
		minIters: c.minIters,
	}
}
460223
func (c *Client) WithSessionToken(sessionToken string) *Client {
	newClient, _ := NewClient(c.appID, c.restApiKey)
	newClient.sessionToken = sessionToken
	newClient.logger = c.logger
	return newClient
}
399699
func (m *ClientDB) CreateClientSession(session *wtdb.ClientSession) error {
	m.mu.Lock()
	defer m.mu.Unlock()

	// Ensure that a session key index has been reserved for this tower.
	keyIndex, ok := m.indexes[session.TowerID]
	if !ok {
		return wtdb.ErrNoReservedKeyIndex
	}

	// Ensure that the session's index matches the reserved index.
	if keyIndex != session.KeyIndex {
		return wtdb.ErrIncorrectKeyIndex
	}

	// Remove the key index reservation for this tower. Once committed, this
	// permits us to create another session with this tower.
	delete(m.indexes, session.TowerID)

	m.activeSessions[session.ID] = &wtdb.ClientSession{
		TowerID:          session.TowerID,
		KeyIndex:         session.KeyIndex,
		ID:               session.ID,
		Policy:           session.Policy,
		SeqNum:           session.SeqNum,
		TowerLastApplied: session.TowerLastApplied,
		RewardPkScript:   cloneBytes(session.RewardPkScript),
		CommittedUpdates: make(map[uint16]*wtdb.CommittedUpdate),
		AckedUpdates:     make(map[uint16]wtdb.BackupID),
	}

	return nil
}
403369
func NewLRUClientSessionCache(capacity int) ClientSessionCache {
	const defaultSessionCacheCapacity = 64

	if capacity < 1 {
		capacity = defaultSessionCacheCapacity
	}
	return &lruSessionCache{
		m:        make(map[string]*list.Element),
		q:        list.New(),
		capacity: capacity,
	}
}
1183674
func (sam *SAM) NewStreamSession(id string, keys I2PKeys, options []string) (*StreamSession, error) {
	conn, err := sam.newGenericSession("STREAM", id, keys, options, []string{})
	if err != nil {
		return nil, err
	}
	return &StreamSession{sam.address, id, conn, keys}, nil
}
626311
func (conn *Connection) CreateSession(username string,
	password string) (*CookieAuth, error) {
	sessUrl, err := buildUrl("_session")
	if err != nil {
		return &CookieAuth{}, err
	}
	var headers = make(map[string]string)
	body := "name=" + username + "&password=" + password
	headers["Content-Type"] = "application/x-www-form-urlencoded"
	resp, err := conn.request("POST", sessUrl,
		strings.NewReader(body), headers, nil)
	if err != nil {
		return &CookieAuth{}, err
	}
	defer resp.Body.Close()
	authToken := func() string {
		for _, cookie := range resp.Cookies() {
			if cookie.Name == "AuthSession" {
				return cookie.Value
			}
		}
		return ""
	}()
	return &CookieAuth{AuthToken: authToken}, nil
}
329695
func newPcapSession(client string, logger logrus.FieldLogger) *pcapSession {
	return &pcapSession{
		client: client,
		logger: logger,
	}
}
208514
func (s *Service) RenewSession(ctx context.Context, session *platform.Session, newExpiration time.Time) error {
	if session == nil {
		return &platform.Error{
			Msg: "session is nil",
		}
	}
	session.ExpiresAt = newExpiration
	return s.PutSession(ctx, session)
}
1155275
func NewLinkWithSession(conn *amqp.Client, session *amqp.Session, address string) (*Link, error) {

	authSender, err := session.NewSender(
		amqp.LinkTargetAddress(address),
	)
	if err != nil {
		return nil, err
	}

	linkID, err := uuid.NewV4()
	if err != nil {
		return nil, err
	}

	id := linkID.String()
	clientAddress := strings.Replace("$", "", address, -1) + replyPostfix + id
	authReceiver, err := session.NewReceiver(
		amqp.LinkSourceAddress(address),
		amqp.LinkTargetAddress(clientAddress),
	)
	if err != nil {
		return nil, err
	}

	return &Link{
		sender:        authSender,
		receiver:      authReceiver,
		session:       session,
		clientAddress: clientAddress,
		id:            id,
	}, nil
}
474552
func NewClientPasswordWithSessionId(baseUrl, username, password, sessionId string, tlsSkipVerify bool) (*http.Client, *AuthResponse, error) {
	sessionId = strings.TrimSpace(sessionId)
	if len(sessionId) > 0 {
		httpClient := NewClientSessionId(sessionId, tlsSkipVerify)
		userUrl := urlutil.JoinAbsolute(baseUrl, RelPathApiUserCurrent)
		resp, err := httpClient.Get(userUrl)
		if err == nil && resp.StatusCode == 200 {
			return httpClient, nil, nil
		}
	}
	return NewClientPassword(baseUrl, username, password, tlsSkipVerify)
}
1286746
func (cfg *Config) DatagramSession() (session *DatagramSession, err error) {
	// connect
	var s *SAM
	s, err = NewSAM(cfg.Addr)
	if err == nil {
		// ensure keys exist
		var keys I2PKeys
		keys, err = s.EnsureKeyfile(cfg.Keyfile)
		if err == nil {
			// determine udp port
			var portstr string
			_, portstr, err = net.SplitHostPort(cfg.Addr)
			if err == nil {
				var port int
				port, err = strconv.Atoi(portstr)
				if err == nil && port > 0 {
					// udp port is 1 lower
					port--
					// create session
					session, err = s.NewDatagramSession(cfg.Session, keys, cfg.Opts.AsList(), port)
				}
			}
		}
	}
	return
}
1220600
func NewSession(name, addr, keyfile string) Session {
	return &samSession{
		name:       name,
		addr:       addr,
		minversion: "3.0",
		maxversion: "3.0",
		keys:       NewKeyfile(keyfile),
	}
}
723811
func NewMemorySessionManager() *MemorySessionManager {
	mgr := new(MemorySessionManager)
	mgr.sessions = make(map[string]*MemorySession)
	mgr.GarbageCollection()
	return mgr
}
875095
func newSession(maxAge int) *Session {
	uid, err := uuid.NewV4()
	if err != nil {
		panic(ErrNoSessionID)
	}
	return &Session{
		true, // is new
		internalSession{
			make(map[string]interface{}),
			uid.String(),
			time.Now(),
			time.Duration(maxAge) * time.Second,
		},
	}
}
251928
func (*TeleportWebSessionMarshaler) UnmarshalWebSession(bytes []byte) (WebSession, error) {
	var h ResourceHeader
	err := json.Unmarshal(bytes, &h)
	if err != nil {
		return nil, trace.Wrap(err)
	}
	switch h.Version {
	case "":
		var ws WebSessionV1
		err := json.Unmarshal(bytes, &ws)
		if err != nil {
			return nil, trace.Wrap(err)
		}
		utils.UTC(&ws.Expires)
		return ws.V2(), nil
	case V2:
		var ws WebSessionV2
		if err := utils.UnmarshalWithSchema(GetWebSessionSchema(), &ws, bytes); err != nil {
			return nil, trace.BadParameter(err.Error())
		}
		utils.UTC(&ws.Spec.BearerTokenExpires)
		utils.UTC(&ws.Spec.Expires)

		if err := ws.CheckAndSetDefaults(); err != nil {
			return nil, trace.Wrap(err)
		}

		return &ws, nil
	}

	return nil, trace.BadParameter("web session resource version %v is not supported", h.Version)
}
239319
func (s *Service) RenewSession(ctx context.Context, session *influxdb.Session, newExpiration time.Time) error {
	if session == nil {
		return &influxdb.Error{
			Msg: "session is nil",
		}
	}
	return s.kv.Update(ctx, func(tx Tx) error {
		session.ExpiresAt = newExpiration
		if err := s.putSession(ctx, tx, session); err != nil {
			return &influxdb.Error{
				Err: err,
			}
		}
		return nil
	})
}