Skip to content

Commit

Permalink
encapsulate cookie logic within AuthService
Browse files Browse the repository at this point in the history
  • Loading branch information
mk6i committed Jun 13, 2024
1 parent e217539 commit 51b4d44
Show file tree
Hide file tree
Showing 18 changed files with 267 additions and 307 deletions.
7 changes: 2 additions & 5 deletions .mockery.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ packages:
OnlineNotifier:
config:
filename: "mock_online_notifier_test.go"
CookieCracker:
config:
filename: "mock_cookie_cracker_test.go"
github.com/mk6i/retro-aim-server/server/http:
interfaces:
UserManager:
Expand Down Expand Up @@ -89,9 +86,9 @@ packages:
LegacyBuddyListManager:
config:
filename: "mock_legacy_buddy_list_manager_test.go"
CookieIssuer:
CookieBaker:
config:
filename: "mock_cookie_issuer_test.go"
filename: "mock_cookie_baker_test.go"
BuddyBroadcaster:
config:
filename: "mock_buddy_broadcaster_test.go"
16 changes: 5 additions & 11 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ func main() {
OServiceHandler: handler.NewOServiceHandler(logger, oServiceService),
PermitDenyHandler: handler.NewPermitDenyHandler(logger, foodgroupService),
}),
CookieCracker: cookieBaker,
Logger: logger,
OnlineNotifier: oServiceService,
ListenAddr: net.JoinHostPort("", cfg.BOSPort),
Expand All @@ -101,7 +100,6 @@ func main() {
}),
Logger: logger,
OnlineNotifier: oServiceService,
CookieCracker: cookieBaker,
}.Start()
wg.Done()
}(logger)
Expand All @@ -124,7 +122,6 @@ func main() {
Logger: logger,
OnlineNotifier: oServiceService,
ListenAddr: net.JoinHostPort("", cfg.ChatNavPort),
CookieCracker: cookieBaker,
}.Start()
wg.Done()
}(logger)
Expand All @@ -142,7 +139,6 @@ func main() {
AlertHandler: handler.NewAlertHandler(logger),
OServiceHandler: handler.NewOServiceHandler(logger, oServiceService),
}),
CookieCracker: cookieBaker,
Logger: logger,
OnlineNotifier: oServiceService,
ListenAddr: net.JoinHostPort("", cfg.AlertPort),
Expand All @@ -158,9 +154,8 @@ func main() {
oServiceService := foodgroup.NewOServiceServiceForBART(cfg, logger, buddyService)

oscar.BOSServer{
AuthService: authService,
Config: cfg,
CookieCracker: cookieBaker,
AuthService: authService,
Config: cfg,
Handler: handler.NewBARTRouter(handler.Handlers{
BARTHandler: handler.NewBARTHandler(logger, bartService),
OServiceHandler: handler.NewOServiceHandler(logger, oServiceService),
Expand All @@ -176,10 +171,9 @@ func main() {
authHandler := foodgroup.NewAuthService(cfg, sessionManager, feedbagStore, chatRegistry, adjListBuddyListStore, cookieBaker, nil)

oscar.AuthServer{
AuthService: authHandler,
Config: cfg,
Logger: logger,
CookieCracker: cookieBaker,
AuthService: authHandler,
Config: cfg,
Logger: logger,
}.Start()
wg.Done()
}(logger)
Expand Down
29 changes: 21 additions & 8 deletions foodgroup/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func NewAuthService(
userManager UserManager,
chatRegistry ChatRegistry,
legacyBuddyListManager LegacyBuddyListManager,
cookieIssuer CookieIssuer,
cookieBaker CookieBaker,
buddyUpdateBroadcaster BuddyBroadcaster,
) *AuthService {
return &AuthService{
Expand All @@ -30,7 +30,7 @@ func NewAuthService(
legacyBuddyListManager: legacyBuddyListManager,
sessionManager: sessionManager,
userManager: userManager,
cookieIssuer: cookieIssuer,
cookieBaker: cookieBaker,
buddyUpdateBroadcaster: buddyUpdateBroadcaster,
}
}
Expand All @@ -42,18 +42,23 @@ type AuthService struct {
buddyUpdateBroadcaster BuddyBroadcaster
chatRegistry ChatRegistry
config config.Config
cookieIssuer CookieIssuer
cookieBaker CookieBaker
legacyBuddyListManager LegacyBuddyListManager
sessionManager SessionManager
userManager UserManager
}

// RegisterChatSession creates and returns a chat room session.
func (s AuthService) RegisterChatSession(loginCookie []byte) (*state.Session, error) {
func (s AuthService) RegisterChatSession(authCookie []byte) (*state.Session, error) {
token, err := s.cookieBaker.Crack(authCookie)
if err != nil {
return nil, err
}
c := chatLoginCookie{}
if err := wire.Unmarshal(&c, bytes.NewBuffer(loginCookie)); err != nil {
if err := wire.Unmarshal(&c, bytes.NewBuffer(token)); err != nil {
return nil, err
}

room, chatSessMgr, err := s.chatRegistry.Retrieve(c.ChatCookie)
if err != nil {
return nil, err
Expand All @@ -65,20 +70,28 @@ func (s AuthService) RegisterChatSession(loginCookie []byte) (*state.Session, er
if u == nil {
return nil, fmt.Errorf("user not found")
}

chatSess := chatSessMgr.(SessionManager).AddSession(u.DisplayScreenName)
chatSess.SetChatRoomCookie(room.Cookie)

return chatSess, nil
}

// RegisterBOSSession creates and returns a user's session.
func (s AuthService) RegisterBOSSession(screenName state.IdentScreenName) (*state.Session, error) {
u, err := s.userManager.User(screenName)
func (s AuthService) RegisterBOSSession(authCookie []byte) (*state.Session, error) {
screenName, err := s.cookieBaker.Crack(authCookie)
if err != nil {
return nil, err
}

u, err := s.userManager.User(state.NewIdentScreenName(string(screenName)))
if err != nil {
return nil, fmt.Errorf("failed to retrieve user: %w", err)
}
if u == nil {
return nil, fmt.Errorf("user not found")
}

return s.sessionManager.AddSession(u.DisplayScreenName), nil
}

Expand Down Expand Up @@ -252,7 +265,7 @@ func (s AuthService) login(
}
}

cookie, err := s.cookieIssuer.Issue([]byte(screenName))
cookie, err := s.cookieBaker.Issue([]byte(screenName))
if err != nil {
return wire.TLVRestBlock{}, fmt.Errorf("failed to make auth cookie: %w", err)
}
Expand Down
85 changes: 52 additions & 33 deletions foodgroup/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,16 +389,16 @@ func TestAuthService_BUCPLoginRequest(t *testing.T) {
Return(params.err)
}
sessionManager := newMockSessionManager(t)
cookieIssuer := newMockCookieIssuer(t)
cookieBaker := newMockCookieBaker(t)
for _, params := range tc.mockParams.cookieIssuerParams {
cookieIssuer.EXPECT().
cookieBaker.EXPECT().
Issue(params.data).
Return(params.cookie, params.err)
}

svc := AuthService{
config: tc.cfg,
cookieIssuer: cookieIssuer,
cookieBaker: cookieBaker,
sessionManager: sessionManager,
userManager: userManager,
}
Expand Down Expand Up @@ -747,15 +747,15 @@ func TestAuthService_FLAPLoginResponse(t *testing.T) {
Return(params.err)
}
sessionManager := newMockSessionManager(t)
cookieIssuer := newMockCookieIssuer(t)
cookieBaker := newMockCookieBaker(t)
for _, params := range tc.mockParams.cookieIssuerParams {
cookieIssuer.EXPECT().
cookieBaker.EXPECT().
Issue(params.data).
Return(params.cookie, params.err)
}
svc := AuthService{
config: tc.cfg,
cookieIssuer: cookieIssuer,
cookieBaker: cookieBaker,
sessionManager: sessionManager,
userManager: userManager,
}
Expand Down Expand Up @@ -936,16 +936,8 @@ func TestAuthService_BUCPChallengeRequest(t *testing.T) {
}

func TestAuthService_RegisterChatSession_HappyPath(t *testing.T) {
cookie := "chat-1234"
sess := newTestSession("screen-name")

c := chatLoginCookie{
ChatCookie: cookie,
ScreenName: sess.IdentScreenName().String(),
}
buf := &bytes.Buffer{}
assert.NoError(t, wire.Marshal(c, buf))

sessionManager := newMockSessionManager(t)
sessionManager.EXPECT().
AddSession(sess.DisplayScreenName()).
Expand All @@ -958,40 +950,57 @@ func TestAuthService_RegisterChatSession_HappyPath(t *testing.T) {
DisplayScreenName: sess.DisplayScreenName(),
}, nil)

chatCookie := "the-chat-cookie"
chatRegistry := newMockChatRegistry(t)
chatRegistry.EXPECT().
Retrieve(cookie).
Retrieve(chatCookie).
Return(state.ChatRoom{}, sessionManager, nil)

cookieIssuer := newMockCookieIssuer(t)
c := chatLoginCookie{
ChatCookie: chatCookie,
ScreenName: sess.IdentScreenName().String(),
}
chatCookieBuf := &bytes.Buffer{}
assert.NoError(t, wire.Marshal(c, chatCookieBuf))

authCookie := []byte("the-auth-cookie")
cookieBaker := newMockCookieBaker(t)
cookieBaker.EXPECT().
Crack(authCookie).
Return(chatCookieBuf.Bytes(), nil)

svc := NewAuthService(config.Config{}, nil, userManager, chatRegistry, nil, cookieIssuer, nil)
svc := NewAuthService(config.Config{}, nil, userManager, chatRegistry, nil, cookieBaker, nil)

have, err := svc.RegisterChatSession(buf.Bytes())
have, err := svc.RegisterChatSession(authCookie)
assert.NoError(t, err)
assert.Equal(t, sess, have)
}

func TestAuthService_RegisterBOSSession_ChatNotFound(t *testing.T) {
cookie := "chat-1234"
func TestAuthService_RegisterChatSession_ChatNotFound(t *testing.T) {
chatCookie := "the-chat-cookie"
sess := newTestSession("screen-name")

c := chatLoginCookie{
ChatCookie: cookie,
ChatCookie: chatCookie,
ScreenName: sess.IdentScreenName().String(),
}
loginCookie := &bytes.Buffer{}
assert.NoError(t, wire.Marshal(c, loginCookie))

chatRegistry := newMockChatRegistry(t)
chatRegistry.EXPECT().
Retrieve(cookie).
Retrieve(chatCookie).
Return(state.ChatRoom{}, nil, state.ErrChatRoomNotFound)

cookieIssuer := newMockCookieIssuer(t)
svc := NewAuthService(config.Config{}, nil, nil, chatRegistry, nil, cookieIssuer, nil)
authCookie := []byte("the-auth-cookie")
cookieBaker := newMockCookieBaker(t)
cookieBaker.EXPECT().
Crack(authCookie).
Return(loginCookie.Bytes(), nil)

svc := NewAuthService(config.Config{}, nil, nil, chatRegistry, nil, cookieBaker, nil)

_, err := svc.RegisterChatSession(loginCookie.Bytes())
_, err := svc.RegisterChatSession(authCookie)
assert.ErrorIs(t, err, state.ErrChatRoomNotFound)
}

Expand All @@ -1003,16 +1012,21 @@ func TestAuthService_RegisterBOSSession_HappyPath(t *testing.T) {
AddSession(sess.DisplayScreenName()).
Return(sess)

cookieIssuer := newMockCookieIssuer(t)
authCookie := []byte(`the-auth-cookie`)

cookieBaker := newMockCookieBaker(t)
cookieBaker.EXPECT().
Crack(authCookie).
Return([]byte("screen-name"), nil)

userManager := newMockUserManager(t)
userManager.EXPECT().
User(sess.IdentScreenName()).
Return(&state.User{DisplayScreenName: sess.DisplayScreenName()}, nil)

svc := NewAuthService(config.Config{}, sessionManager, userManager, nil, nil, cookieIssuer, nil)
svc := NewAuthService(config.Config{}, sessionManager, userManager, nil, nil, cookieBaker, nil)

have, err := svc.RegisterBOSSession(sess.IdentScreenName())
have, err := svc.RegisterBOSSession(authCookie)
assert.NoError(t, err)
assert.Equal(t, sess, have)
}
Expand All @@ -1025,16 +1039,21 @@ func TestAuthService_RegisterBOSSession_SessionNotFound(t *testing.T) {
AddSession(sess.DisplayScreenName()).
Return(nil)

cookieIssuer := newMockCookieIssuer(t)
authCookie := []byte(`the-auth-cookie`)
cookieBaker := newMockCookieBaker(t)

cookieBaker.EXPECT().
Crack(authCookie).
Return([]byte("screen-name"), nil)

userManager := newMockUserManager(t)
userManager.EXPECT().
User(sess.IdentScreenName()).
Return(&state.User{DisplayScreenName: sess.DisplayScreenName()}, nil)

svc := NewAuthService(config.Config{}, sessionManager, userManager, nil, nil, cookieIssuer, nil)
svc := NewAuthService(config.Config{}, sessionManager, userManager, nil, nil, cookieBaker, nil)

have, err := svc.RegisterBOSSession(sess.IdentScreenName())
have, err := svc.RegisterBOSSession(authCookie)
assert.NoError(t, err)
assert.Nil(t, have)
}
Expand Down Expand Up @@ -1172,9 +1191,9 @@ func TestAuthService_SignoutChat(t *testing.T) {
Retrieve(tt.chatRoom.Cookie).
Return(tt.chatRoom, chatSessionManager, tt.wantErr)

cookieIssuer := newMockCookieIssuer(t)
cookieBaker := newMockCookieBaker(t)

svc := NewAuthService(config.Config{}, nil, nil, chatRegistry, nil, cookieIssuer, nil)
svc := NewAuthService(config.Config{}, nil, nil, chatRegistry, nil, cookieBaker, nil)

err := svc.SignoutChat(nil, tt.userSession)
assert.ErrorIs(t, err, tt.wantErr)
Expand Down
Loading

0 comments on commit 51b4d44

Please sign in to comment.