Skip to content

Commit

Permalink
issue #60 - make service request return bos cookie
Browse files Browse the repository at this point in the history
  • Loading branch information
mk6i committed Aug 17, 2024
1 parent 9b3d49e commit f967e85
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
33 changes: 22 additions & 11 deletions foodgroup/oservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -574,9 +574,19 @@ type chatLoginCookie struct {
// ServiceRequest handles service discovery, providing a host name and metadata
// for connecting to the food group service specified in inFrame.
func (s OServiceServiceForBOS) ServiceRequest(ctx context.Context, sess *state.Session, inFrame wire.SNACFrame, inBody wire.SNAC_0x01_0x04_OServiceServiceRequest) (wire.SNACMessage, error) {
fnIssueCookie := func(val any) ([]byte, error) {
buf := &bytes.Buffer{}
if err := wire.MarshalBE(val, buf); err != nil {
return nil, err
}
return s.cookieIssuer.Issue(buf.Bytes())
}

switch inBody.FoodGroup {
case wire.Admin:
cookie, err := s.cookieIssuer.Issue([]byte(sess.IdentScreenName().String()))
cookie, err := fnIssueCookie(bosCookie{
ScreenName: sess.DisplayScreenName(),
})
if err != nil {
return wire.SNACMessage{}, err
}
Expand All @@ -599,7 +609,9 @@ func (s OServiceServiceForBOS) ServiceRequest(ctx context.Context, sess *state.S
},
}, nil
case wire.Alert:
cookie, err := s.cookieIssuer.Issue([]byte(sess.IdentScreenName().String()))
cookie, err := fnIssueCookie(bosCookie{
ScreenName: sess.DisplayScreenName(),
})
if err != nil {
return wire.SNACMessage{}, err
}
Expand All @@ -622,7 +634,9 @@ func (s OServiceServiceForBOS) ServiceRequest(ctx context.Context, sess *state.S
},
}, nil
case wire.BART:
cookie, err := s.cookieIssuer.Issue([]byte(sess.IdentScreenName().String()))
cookie, err := fnIssueCookie(bosCookie{
ScreenName: sess.DisplayScreenName(),
})
if err != nil {
return wire.SNACMessage{}, err
}
Expand All @@ -645,7 +659,9 @@ func (s OServiceServiceForBOS) ServiceRequest(ctx context.Context, sess *state.S
},
}, nil
case wire.ChatNav:
cookie, err := s.cookieIssuer.Issue([]byte(sess.IdentScreenName().String()))
cookie, err := fnIssueCookie(bosCookie{
ScreenName: sess.DisplayScreenName(),
})
if err != nil {
return wire.SNACMessage{}, err
}
Expand Down Expand Up @@ -683,15 +699,10 @@ func (s OServiceServiceForBOS) ServiceRequest(ctx context.Context, sess *state.S
return wire.SNACMessage{}, fmt.Errorf("unable to retrieve room info: %w", err)
}

loginCookie := chatLoginCookie{
cookie, err := fnIssueCookie(chatLoginCookie{
ChatCookie: room.Cookie(),
ScreenName: sess.DisplayScreenName(),
}
buf := &bytes.Buffer{}
if err := wire.MarshalBE(loginCookie, buf); err != nil {
return wire.SNACMessage{}, err
}
cookie, err := s.cookieIssuer.Issue(buf.Bytes())
})
if err != nil {
return wire.SNACMessage{}, err
}
Expand Down
12 changes: 9 additions & 3 deletions foodgroup/oservice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ func TestOServiceServiceForBOS_ServiceRequest(t *testing.T) {
cookieBakerParams: cookieBakerParams{
cookieIssueParams: cookieIssueParams{
{
dataIn: []byte("user_screen_name"),
dataIn: []byte{
0x10, 'u', 's', 'e', 'r', '_', 's', 'c', 'r', 'e', 'e', 'n', '_', 'n', 'a', 'm', 'e',
},
cookieOut: []byte("the-cookie"),
},
},
Expand Down Expand Up @@ -141,7 +143,9 @@ func TestOServiceServiceForBOS_ServiceRequest(t *testing.T) {
cookieBakerParams: cookieBakerParams{
cookieIssueParams: cookieIssueParams{
{
dataIn: []byte("user_screen_name"),
dataIn: []byte{
0x10, 'u', 's', 'e', 'r', '_', 's', 'c', 'r', 'e', 'e', 'n', '_', 'n', 'a', 'm', 'e',
},
cookieOut: []byte("the-cookie"),
},
},
Expand Down Expand Up @@ -185,7 +189,9 @@ func TestOServiceServiceForBOS_ServiceRequest(t *testing.T) {
cookieBakerParams: cookieBakerParams{
cookieIssueParams: cookieIssueParams{
{
dataIn: []byte("user_screen_name"),
dataIn: []byte{
0x10, 'u', 's', 'e', 'r', '_', 's', 'c', 'r', 'e', 'e', 'n', '_', 'n', 'a', 'm', 'e',
},
cookieOut: []byte("the-cookie"),
},
},
Expand Down

0 comments on commit f967e85

Please sign in to comment.