MCPcopy Create free account
hub / github.com/daodst/chat / validateTopicName

Method validateTopicName

server/session.go:565–605  ·  view source on GitHub ↗

validateTopicName expands session specific topic name to global name Returns topic: session-specific topic name the message recepient should see routeTo: routable global topic name err: *ServerComMessage with an error to return to the sender

(msgId, topic string, timestamp time.Time)

Source from the content-addressed store, hash-verified

563// routeTo: routable global topic name
564// err: *ServerComMessage with an error to return to the sender
565func (s *Session) validateTopicName(msgId, topic string, timestamp time.Time) (string, string, *ServerComMessage) {
566
567 if topic == "" {
568 return "", "", ErrMalformed(msgId, "", timestamp)
569 }
570
571 if !strings.HasPrefix(topic, "grp") && s.uid.IsZero() {
572 // me and p2p topics require authentication
573 return "", "", ErrAuthRequired(msgId, topic, timestamp)
574 }
575
576 // Topic to route to i.e. rcptto: or s.subs[routeTo]
577 routeTo := topic
578
579 if topic == "me" {
580 routeTo = s.uid.UserId()
581 } else if strings.HasPrefix(topic, "usr") {
582 // packet to a specific user
583 uid2 := types.ParseUserId(topic)
584 if uid2.IsZero() {
585 // Ensure the user id is valid
586 return "", "", ErrMalformed(msgId, topic, timestamp)
587 } else if uid2 == s.uid {
588 // Use 'me' to access self-topic
589 return "", "", ErrPermissionDenied(msgId, topic, timestamp)
590 }
591 routeTo = s.uid.P2PName(uid2)
592 topic = s.uid.UserId() // but the echo message should come back as uid2.Name()
593 } else if strings.HasPrefix(topic, "p2p") {
594 uid1, uid2, err := types.ParseP2P(topic)
595 if err != nil || uid1.IsZero() || uid2.IsZero() || uid1 == uid2 {
596 // Ensure the user ids are valid
597 return "", "", ErrMalformed(msgId, topic, timestamp)
598 } else if uid1 != s.uid && uid2 != s.uid {
599 // One can't access someone else's p2p topic
600 return "", "", ErrPermissionDenied(msgId, topic, timestamp)
601 }
602 }
603
604 return topic, routeTo, nil
605}

Callers 5

subscribeMethod · 0.95
publishMethod · 0.95
getMethod · 0.95
setMethod · 0.95
delMethod · 0.95

Calls 3

ErrMalformedFunction · 0.85
ErrAuthRequiredFunction · 0.85
ErrPermissionDeniedFunction · 0.85

Tested by

no test coverage detected