// UserSession retrieves and returns the SessionManager's CurrentSession field.
// Nil is returned if the session is not authenticated.
486950
func (sm *Manager) UserSession(ctx context.Context) (*types.UserSession, error) {
	var mgr mo.SessionManager

	pc := property.DefaultCollector(sm.client)
	err := pc.RetrieveOne(ctx, sm.Reference(), []string{"currentSession"}, &mgr)
	if err != nil {
		// It's OK if we can't retrieve properties because we're not authenticated
		if f, ok := err.(types.HasFault); ok {
			switch f.Fault().(type) {
			case *types.NotAuthenticated:
				return nil, nil
			}
		}

		return nil, err
	}

	return mgr.CurrentSession, nil
}
278209
func (m *Module) CreateSession(userToken string, rw http.ResponseWriter) error {
	cookie, err := m.newSessionCookie(&UserSession{
		ID:        userToken,
		SessionID: fmt.Sprintf("%s-%d", userToken, time.Now().UnixNano()),
	})
	if err != nil {
		return err
	}
	http.SetCookie(rw, cookie)
	return nil
}
600507
func (c *Crowd) GetSession(token string) (s Session, err error) {
	c.Client.Jar = c.cookies
	url := c.url + "rest/usermanagement/1/session/" + token
	req, err := http.NewRequest("GET", url, nil)
	if err != nil {
		return s, err
	}
	req.SetBasicAuth(c.user, c.passwd)
	req.Header.Set("Accept", "application/xml")
	req.Header.Set("Content-Type", "application/xml")
	resp, err := c.Client.Do(req)
	if err != nil {
		return s, err
	}
	defer resp.Body.Close()

	switch resp.StatusCode {
	case 404:
		return s, fmt.Errorf("session not found")
	case 200:
		// fall through switch without returning
	default:
		return s, fmt.Errorf("request failed: %s", resp.Status)
	}

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return s, err
	}

	err = xml.Unmarshal(body, &s)
	if err != nil {
		return s, err
	}

	return s, nil
}
688463
func (ctx *Context) Session() *sessions.Session {
	// this help us if we call `Session()` multiple times in the same handler
	if ctx.session == nil {
		// start a new session if not created before.
		ctx.session = owner.sessionsManager.Start(ctx.Context)
	}

	return ctx.session
}
119332
func NewAuthenticatedSession(user, password string) (*Client, error) {
	client, err := NewClient()
	if err != nil {
		return nil, err
	}

	if err := client.Authenticate(user, password); err != nil {
		return nil, err
	}

	return client, nil
}
843065
func (c *Context) SetSession(session Session, login bool) {
	session.UserAgent = c.req.UserAgent()
	session.IpAddress = strings.Split(c.req.RemoteAddr, ":")[0]
	session.LastActiveTime = time.Now()
	session.CallCount++

	c.svc.sm.sessions[session.Key] = session
	c.Session = &session

	if login {
		http.SetCookie(c.res, &http.Cookie{
			Name:  soap.SessionCookieName,
			Value: session.Key,
		})

		c.postEvent(&types.UserLoginSessionEvent{
			SessionId: session.Key,
			IpAddress: session.IpAddress,
			UserAgent: session.UserAgent,
			Locale:    session.Locale,
		})
	}
}
949713
func (s *SSO) StartSession(w http.ResponseWriter, cs crowd.Session) {
	ck := http.Cookie{
		Name:    s.CookieConfig.Name,
		Domain:  s.CookieConfig.Domain,
		Secure:  s.CookieConfig.Secure,
		Value:   cs.Token,
		Expires: cs.Expires,
	}
	http.SetCookie(w, &ck)
}
6870
func (s *ScanService) TakeSession(fn SessionFunction) ScanFunction {
	return func(req *restful.Request, resp *restful.Response, sc *scan.Scan) {
		id := req.PathParameter(SessionParamId)
		if !s.IsId(id) {
			resp.WriteServiceError(http.StatusBadRequest, services.IdHexErr)
			return
		}

		objId := manager.ToId(id)

		sess := sc.GetSession(objId)
		if sess == nil {
			resp.WriteErrorString(http.StatusNotFound, "Not found")
			return
		}
		fn(req, resp, sc, sess)
	}
}
74457
func CreateSession(info infoschema.InfoSchema) (Session, error) {
	s, err := createSession(info)
	if err != nil {
		return nil, errors.Trace(err)
	}

	return s, nil
}
1104911
func (db Db) Session() *DbSession {
	return &DbSession{db: db, Session: db.mainSession.Copy()}
}
1016460
func (u *UniqueSession) OnUserBind(uid, fid string) {
	if u.server.ID == fid {
		return
	}
	oldSession := session.GetSessionByUID(uid)
	if oldSession != nil {
		// TODO: it would be nice to set this correctly
		oldSession.Kick(context.Background())
	}
}
588077
func (s *SessionService) CreateSession(ctx context.Context, user string) (*platform.Session, error) {
	return s.CreateSessionFn(ctx, user)
}
750488
func (mgr *MemorySessionManager) Session(sid string) (Session, error) {
	mgr.lock.RLock()
	if s, ok := mgr.sessions[sid]; ok {
		s.createdAt = time.Now()
		mgr.lock.RUnlock()
		return s, nil
	}
	mgr.lock.RUnlock()
	return nil, fmt.Errorf("Can not retrieve session with id %s", sid)
}
297241
func (u *UserV2) WebSessionInfo(allowedLogins []string) interface{} {
	out := u.V1()
	out.AllowedLogins = allowedLogins
	return *out
}
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,
		},
	}
}
148139
func (a *authHandler) loadSession(r *http.Request) error {
	value := r.Header[a.headerKey]
	var id string

	if len(value) > 0 {
		id = value[0]
	} else {
		id = r.URL.Query().Get("session")
	}

	if id == "" {
		return fmt.Errorf("Header %s and session was empty", a.headerKey)
	}

	ses, err := a.backend.Read(id)

	if err != nil {
		return err
	}

	context.Set(r, a.contextKey, ses)
	return nil
}
1060445
func GetSession() *mgo.Session {
	for {
		if session := NewSession(); session != nil {
			return session
		}

		time.Sleep(1 * time.Second)
	}
}