//Destroy a session (i.e., logout) func (am *AuthManager) Destroy(session *Session) error { authType := session.AuthType authenticator := am.getAuthenticator(authType) err := authenticator.DestroySession(session.Id) if err != nil { return err } //Remove the session node from etcd kapi := registry.GetEtcdKeyAPI() sessionLocation := sessionsLocation + session.Id ctx, _ := context.WithTimeout(context.Background(), 2*time.Second) _, err = kapi.Delete(ctx, sessionLocation, nil) return err }
//Get a session func (am *AuthManager) ReadSession(sessionId string) (*Session, error) { kapi := registry.GetEtcdKeyAPI() ctx, _ := context.WithTimeout(context.Background(), 2*time.Second) sessionLocation := sessionsLocation + sessionId resp, err := kapi.Get(ctx, sessionLocation, &etcd.GetOptions{Recursive: false}) if err != nil { return nil, err } sess, err := am.processResponse(resp) if err != nil { return nil, err } else { return sess, nil } }
//Store the session to etcd func (am *AuthManager) saveSession(sess *Session) error { kapi := registry.GetEtcdKeyAPI() ttl := time.Duration(config.Auth.SessionTimeout) * time.Second sessBytes, err := json.Marshal(sess) sessStr := string(sessBytes) if err != nil { return err } _, err = kapi.Set(context.Background(), sessionsLocation+sess.Id, sessStr, &etcd.SetOptions{TTL: ttl}) if err != nil { return err } return nil }