コード例 #1
0
ファイル: cache.go プロジェクト: desertbit/bitmonster
func setCachedCurrentUser(s *bitmonster.Socket, user *User) {
	// Set the socket value.
	s.SetValue(cacheUserSocketValueKey, user)

	// Remove the cached value after the timeout.
	s.DeleteValueAfterTimeout(cacheUserSocketValueKey, clearCacheInterval)
}
コード例 #2
0
ファイル: auth.go プロジェクト: desertbit/bitmonster
func checkSocketAuthentication(s *bitmonster.Socket) {
	// Get the auth socket value.
	av := getAuthSocketValue(s)
	if av == nil {
		return
	}

	// Skip if not authenticated.
	if !av.isAuth {
		return
	}

	// Debug log.
	log.L.WithFields(logrus.Fields{
		"remoteAddress": s.RemoteAddr(),
	}).Debugf("auth: reauthentication requested")

	// Trigger the event to reauthenticate.
	eventReauth.TriggerSocket(s)

	// Start a timeout to logout the socket session.
	av.reauthTimer = time.AfterFunc(reauthTimeout, func() {
		// Reset the socket authentication values.
		resetAuthSocketValue(s)
	})
}
コード例 #3
0
ファイル: cache.go プロジェクト: desertbit/bitmonster
// getCachedCurrentUser returns the current cached user value or nil.
func getCachedCurrentUser(s *bitmonster.Socket) *User {
	// Obtain the user from the cache if present.
	userI := s.Value(cacheUserSocketValueKey)
	if userI == nil {
		return nil
	}

	// Assertion.
	user, ok := userI.(*User)
	if !ok {
		return nil
	}

	return user
}
コード例 #4
0
ファイル: auth.go プロジェクト: desertbit/bitmonster
// getAuthSocketValue obtains the authentication socket value from the socket.
// If not present, then this function creates the value.
func getAuthSocketValue(s *bitmonster.Socket) *authSocketValue {
	// Get or create the value.
	v := s.Value(authSocketValueKey, func() interface{} {
		// Not present. Create it.
		return newAuthSocketValue()
	})

	// Cast.
	av, ok := v.(*authSocketValue)
	if !ok {
		return nil
	}

	return av
}
コード例 #5
0
ファイル: auth.go プロジェクト: desertbit/bitmonster
func onNewSocket(s *bitmonster.Socket) {
	// Start a goroutine to check the authentication state in an interval.
	go func() {
		ticker := time.NewTicker(checkAuthInterval)
		defer ticker.Stop()

		for {
			select {
			case <-s.ClosedChan():
				// Just release this goroutine.
				return

			case <-ticker.C:
				checkSocketAuthentication(s)
			}
		}
	}()
}
コード例 #6
0
ファイル: auth.go プロジェクト: desertbit/bitmonster
// resetAuthSocketValue removes the socket authentication values and triggers
// the socket Check method.
func resetAuthSocketValue(s *bitmonster.Socket) {
	// Debug log.
	log.L.WithFields(logrus.Fields{
		"remoteAddress": s.RemoteAddr(),
	}).Debugf("auth: authentication state resetted")

	// Get the auth socket value.
	av := getAuthSocketValue(s)
	if av != nil {
		// Stop the reauth timer if present.
		if av.reauthTimer != nil {
			av.reauthTimer.Stop()
		}
	}

	// Remove the socket auth value to delete the authenticated infos.
	s.DeleteValue(authSocketValueKey)

	// Clear the cache.
	clearCache(s)

	// Rerun the event hooks.
	// This will unbind events which require authentication.
	s.Check()
}
コード例 #7
0
ファイル: cache.go プロジェクト: desertbit/bitmonster
// clearCache clears all cached values of the socket.
func clearCache(s *bitmonster.Socket) {
	s.DeleteValue(cacheUserSocketValueKey)
}
コード例 #8
0
ファイル: sample.go プロジェクト: desertbit/bitmonster
func onNewSocket(s *bitmonster.Socket) {
	fmt.Printf("new socket: %s", s.ID())
}