Skip to content

Commit

Permalink
add support for user-defined screen name formatting
Browse files Browse the repository at this point in the history
This commit introduces two distinct screen name data types:
- IdentScreenName: A normalized version of the screen name used as a
unique identifier. This is stored in the database and used for login
validation, ensuring that users can log in regardless of the format
they use.
- DisplayScreenName: Maintains the user-defined formatting, including
case and spaces, allowing for personalized display.

Additionally, a database migration is included, which adds a new field
identScreenName, for storing the IdentScreenName. The screenName
field is renamed to displayScreenName, which stores the value of
DisplayScreenName.

These changes allow users to register and log in with any screen name
format, as long as the normalized version matches the identifier
stored in the database.

From the API perspective, users continue to have the "screen_name"
field, which is the display screen name.
  • Loading branch information
mk6i committed Jun 11, 2024
1 parent ca608a1 commit e217539
Show file tree
Hide file tree
Showing 47 changed files with 1,373 additions and 1,057 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ Invoke-WebRequest -Uri http://localhost:8080/user -Method Get

```powershell
Invoke-WebRequest -Uri http://localhost:8080/user `
-Body '{"screen_name":"myscreenname", "password":"thepassword"}' `
-Body '{"screen_name":"MyScreenName", "password":"thepassword"}' `
-Method Post `
-ContentType "application/json"
```
Expand All @@ -83,7 +83,7 @@ Invoke-WebRequest -Uri http://localhost:8080/user `

```powershell
Invoke-WebRequest -Uri http://localhost:8080/user/password `
-Body '{"screen_name":"myscreenname", "password":"thenewpassword"}' `
-Body '{"screen_name":"MyScreenName", "password":"thenewpassword"}' `
-Method Put `
-ContentType "application/json"
```
Expand All @@ -107,7 +107,7 @@ curl http://localhost:8080/user
#### Create Users

```shell
curl -d'{"screen_name":"myscreenname", "password":"thepassword"}' http://localhost:8080/user
curl -d'{"screen_name":"MyScreenName", "password":"thepassword"}' http://localhost:8080/user
```

#### Delete Users
Expand All @@ -119,7 +119,7 @@ curl -X DELETE -d '{"screen_name": "user123"}' http://localhost:8080/user
#### Change Password

```shell
curl -X PUT -d'{"screen_name":"myscreenname", "password":"thenewpassword"}' http://localhost:8080/user/password
curl -X PUT -d'{"screen_name":"MyScreenName", "password":"thenewpassword"}' http://localhost:8080/user/password
```

#### List Active Sessions
Expand Down
37 changes: 24 additions & 13 deletions foodgroup/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,28 @@ func (s AuthService) RegisterChatSession(loginCookie []byte) (*state.Session, er
if err != nil {
return nil, err
}
chatSess := chatSessMgr.(SessionManager).AddSession(c.ScreenName)
u, err := s.userManager.User(state.NewIdentScreenName(c.ScreenName))
if err != nil {
return nil, fmt.Errorf("failed to retrieve user: %w", err)
}
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(sessionID string) (*state.Session, error) {
return s.sessionManager.AddSession(sessionID), nil
func (s AuthService) RegisterBOSSession(screenName state.IdentScreenName) (*state.Session, error) {
u, err := s.userManager.User(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
}

// Signout removes this user's session and notifies users who have this user on
Expand All @@ -75,7 +89,7 @@ func (s AuthService) Signout(ctx context.Context, sess *state.Session) error {
return err
}
s.sessionManager.RemoveSession(sess)
s.legacyBuddyListManager.DeleteUser(sess.ScreenName())
s.legacyBuddyListManager.DeleteUser(sess.IdentScreenName())
return nil
}

Expand Down Expand Up @@ -113,7 +127,7 @@ func (s AuthService) BUCPChallenge(

var authKey string

user, err := s.userManager.User(screenName)
user, err := s.userManager.User(state.NewIdentScreenName(screenName))
if err != nil {
return wire.SNACMessage{}, err
}
Expand Down Expand Up @@ -163,10 +177,7 @@ func (s AuthService) BUCPChallenge(
// (wire.LoginTLVTagsReconnectHere) and an authorization cookie
// (wire.LoginTLVTagsAuthorizationCookie). Else, an error code is set
// (wire.LoginTLVTagsErrorSubcode).
func (s AuthService) BUCPLogin(
bodyIn wire.SNAC_0x17_0x02_BUCPLoginRequest,
newUserFn func(screenName string) (state.User, error),
) (wire.SNACMessage, error) {
func (s AuthService) BUCPLogin(bodyIn wire.SNAC_0x17_0x02_BUCPLoginRequest, newUserFn func(screenName state.DisplayScreenName) (state.User, error)) (wire.SNACMessage, error) {

block, err := s.login(bodyIn.TLVList, newUserFn)
if err != nil {
Expand Down Expand Up @@ -194,23 +205,23 @@ func (s AuthService) BUCPLogin(
// (wire.LoginTLVTagsReconnectHere) and an authorization cookie
// (wire.LoginTLVTagsAuthorizationCookie). Else, an error code is set
// (wire.LoginTLVTagsErrorSubcode).
func (s AuthService) FLAPLogin(frame wire.FLAPSignonFrame, newUserFn func(screenName string) (state.User, error)) (wire.TLVRestBlock, error) {
func (s AuthService) FLAPLogin(frame wire.FLAPSignonFrame, newUserFn func(screenName state.DisplayScreenName) (state.User, error)) (wire.TLVRestBlock, error) {
return s.login(frame.TLVList, newUserFn)
}

// login validates a user's credentials and creates their session. it returns
// metadata used in both BUCP and FLAP authentication responses.
func (s AuthService) login(
TLVList wire.TLVList,
newUserFn func(screenName string) (state.User, error),
newUserFn func(screenName state.DisplayScreenName) (state.User, error),
) (wire.TLVRestBlock, error) {

screenName, found := TLVList.String(wire.LoginTLVTagsScreenName)
if !found {
return wire.TLVRestBlock{}, errors.New("screen name doesn't exist in tlv")
}

user, err := s.userManager.User(screenName)
user, err := s.userManager.User(state.NewIdentScreenName(screenName))
if err != nil {
return wire.TLVRestBlock{}, err
}
Expand All @@ -230,7 +241,7 @@ func (s AuthService) login(
if !loginOK {
// make login succeed anyway. create new user if the account
// doesn't already exist.
newUser, err := newUserFn(screenName)
newUser, err := newUserFn(state.DisplayScreenName(screenName))
if err != nil {
return wire.TLVRestBlock{}, err
}
Expand Down
Loading

0 comments on commit e217539

Please sign in to comment.