id.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package wire
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "strings"
  6. "go.step.sm/crypto/kms/uri"
  7. )
  8. type ID struct {
  9. Name string `json:"name,omitempty"`
  10. Domain string `json:"domain,omitempty"`
  11. ClientID string `json:"client-id,omitempty"`
  12. Handle string `json:"handle,omitempty"`
  13. }
  14. func ParseID(data []byte) (wireID ID, err error) {
  15. err = json.Unmarshal(data, &wireID)
  16. return
  17. }
  18. type ClientID struct {
  19. Scheme string
  20. Username string
  21. DeviceID string
  22. Domain string
  23. }
  24. // ParseClientID parses a Wire clientID. The ClientID format is as follows:
  25. //
  26. // "wireapp://CzbfFjDOQrenCbDxVmgnFw!594930e9d50bb175@wire.com",
  27. //
  28. // where '!' is used as a separator between the user id & device id.
  29. func ParseClientID(clientID string) (ClientID, error) {
  30. clientIDURI, err := uri.Parse(clientID)
  31. if err != nil {
  32. return ClientID{}, fmt.Errorf("invalid Wire client ID URI %q: %w", clientID, err)
  33. }
  34. if clientIDURI.Scheme != "wireapp" {
  35. return ClientID{}, fmt.Errorf("invalid Wire client ID scheme %q; expected \"wireapp\"", clientIDURI.Scheme)
  36. }
  37. fullUsername := clientIDURI.User.Username()
  38. parts := strings.SplitN(fullUsername, "!", 2)
  39. if len(parts) != 2 {
  40. return ClientID{}, fmt.Errorf("invalid Wire client ID username %q", fullUsername)
  41. }
  42. return ClientID{
  43. Scheme: clientIDURI.Scheme,
  44. Username: parts[0],
  45. DeviceID: parts[1],
  46. Domain: clientIDURI.Host,
  47. }, nil
  48. }