|
| 1 | +package slack |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "net/url" |
| 8 | + "regexp" |
| 9 | + "strconv" |
| 10 | + "strings" |
| 11 | + |
| 12 | + "github.com/nextdotid/proof_server/config" |
| 13 | + types "github.com/nextdotid/proof_server/types" |
| 14 | + util "github.com/nextdotid/proof_server/util" |
| 15 | + mycrypto "github.com/nextdotid/proof_server/util/crypto" |
| 16 | + "github.com/sirupsen/logrus" |
| 17 | + "github.com/slack-go/slack" |
| 18 | + slackClient "github.com/slack-go/slack" |
| 19 | + "golang.org/x/xerrors" |
| 20 | + |
| 21 | + validator "github.com/nextdotid/proof_server/validator" |
| 22 | +) |
| 23 | + |
| 24 | +// Slack represents the validator for Slack platform |
| 25 | +type Slack struct { |
| 26 | + *validator.Base |
| 27 | +} |
| 28 | + |
| 29 | +const ( |
| 30 | + matchTemplate = "^Sig: (.*)$" |
| 31 | +) |
| 32 | + |
| 33 | +var ( |
| 34 | + client *slackClient.Client |
| 35 | + l = logrus.WithFields(logrus.Fields{"module": "validator", "validator": "slack"}) |
| 36 | + re = regexp.MustCompile(matchTemplate) |
| 37 | + postStruct = map[string]string{ |
| 38 | + "default": "🎭 Verifying my Slack ID @%s for @NextDotID.\nSig: %%SIG_BASE64%%\n\nPowered by Next.ID - Connect All Digital Identities.\n", |
| 39 | + "en_US": "🎭 Verifying my Slack ID @%s for @NextDotID.\nSig: %%SIG_BASE64%%\n\nPowered by Next.ID - Connect All Digital Identities.\n", |
| 40 | + "zh_CN": "🎭 正在通过 @NextDotID 验证我的 Slack 帐号 @%s 。\nSig: %%SIG_BASE64%%\n\n由 Next.ID 支持 - 连接全域数字身份。\n", |
| 41 | + } |
| 42 | +) |
| 43 | + |
| 44 | +// Init initializes the Slack validator |
| 45 | +func Init() { |
| 46 | + initClient() |
| 47 | + if validator.PlatformFactories == nil { |
| 48 | + validator.PlatformFactories = make(map[types.Platform]func(*validator.Base) validator.IValidator) |
| 49 | + } |
| 50 | + validator.PlatformFactories[types.Platforms.Slack] = func(base *validator.Base) validator.IValidator { |
| 51 | + slack := &Slack{base} |
| 52 | + return slack |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +// GeneratePostPayload generates the post payload for Slack |
| 57 | +func (s *Slack) GeneratePostPayload() (post map[string]string) { |
| 58 | + post = make(map[string]string) |
| 59 | + for langCode, template := range postStruct { |
| 60 | + post[langCode] = fmt.Sprintf(template, s.Identity) |
| 61 | + } |
| 62 | + return post |
| 63 | +} |
| 64 | + |
| 65 | +// GenerateSignPayload generates the signature payload for Slack |
| 66 | +func (slack *Slack) GenerateSignPayload() (payload string) { |
| 67 | + payloadStruct := validator.H{ |
| 68 | + "action": string(slack.Action), |
| 69 | + "identity": slack.Identity, |
| 70 | + "platform": "slack", |
| 71 | + "created_at": util.TimeToTimestampString(slack.CreatedAt), |
| 72 | + "uuid": slack.Uuid.String(), |
| 73 | + } |
| 74 | + if slack.Previous != "" { |
| 75 | + payloadStruct["prev"] = slack.Previous |
| 76 | + } |
| 77 | + |
| 78 | + payloadBytes, err := json.Marshal(payloadStruct) |
| 79 | + if err != nil { |
| 80 | + l.Warnf("Error when marshaling struct: %s", err.Error()) |
| 81 | + return "" |
| 82 | + } |
| 83 | + |
| 84 | + return string(payloadBytes) |
| 85 | +} |
| 86 | + |
| 87 | +func (slack *Slack) Validate() (err error) { |
| 88 | + initClient() |
| 89 | + slack.Identity = strings.ToLower(slack.Identity) |
| 90 | + slack.SignaturePayload = slack.GenerateSignPayload() |
| 91 | + |
| 92 | + if slack.Action == types.Actions.Delete { |
| 93 | + return mycrypto.ValidatePersonalSignature(slack.SignaturePayload, slack.Signature, slack.Pubkey) |
| 94 | + } |
| 95 | + |
| 96 | + u, err := url.Parse(slack.ProofLocation) |
| 97 | + if err != nil { |
| 98 | + return xerrors.Errorf("Error when parsing slack proof location: %v", err) |
| 99 | + } |
| 100 | + msgPath := strings.Trim(u.Path, "/") |
| 101 | + parts := strings.Split(msgPath, "/") |
| 102 | + if len(parts) != 2 { |
| 103 | + return xerrors.Errorf("Error: malformatted slack proof location: %v", slack.ProofLocation) |
| 104 | + } |
| 105 | + channelID := parts[0] |
| 106 | + messageID, err := strconv.ParseInt(parts[1], 10, 64) |
| 107 | + if err != nil { |
| 108 | + return xerrors.Errorf("Error when parsing slack message ID %s: %s", slack.ProofLocation, err.Error()) |
| 109 | + } |
| 110 | + const ( |
| 111 | + maxPages = 10 // maximum number of pages to fetch |
| 112 | + historyPageSize = 1000 // Slack API max limit per page |
| 113 | + ) |
| 114 | + pageCount := 0 |
| 115 | + var foundMsg *slackClient.Message |
| 116 | + var latestTs string |
| 117 | + |
| 118 | + for { |
| 119 | + if pageCount >= maxPages { |
| 120 | + return xerrors.Errorf("Reached max number of pages (%d) while searching for message with ID %d in conversation history", maxPages, messageID) |
| 121 | + } |
| 122 | + |
| 123 | + // Get conversation history |
| 124 | + history, err := client.GetConversationHistory(&slackClient.GetConversationHistoryParameters{ |
| 125 | + ChannelID: channelID, |
| 126 | + Latest: latestTs, |
| 127 | + Inclusive: true, |
| 128 | + Oldest: "", |
| 129 | + Limit: historyPageSize, |
| 130 | + }) |
| 131 | + if err != nil { |
| 132 | + return xerrors.Errorf("Error getting the conversation history from slack: %w", err) |
| 133 | + } |
| 134 | + |
| 135 | + for _, msg := range history.Messages { |
| 136 | + if msg.ClientMsgID == strconv.FormatInt(messageID, 10) { |
| 137 | + foundMsg = &msg |
| 138 | + break |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + if foundMsg != nil { |
| 143 | + break |
| 144 | + } |
| 145 | + |
| 146 | + if !history.HasMore { |
| 147 | + return xerrors.Errorf("Could not find message with ID %d in conversation history", messageID) |
| 148 | + } |
| 149 | + |
| 150 | + latestTs = history.Messages[len(history.Messages)-1].Timestamp |
| 151 | + pageCount++ |
| 152 | + } |
| 153 | + |
| 154 | + userInt, err := strconv.ParseInt(foundMsg.User, 10, 64) |
| 155 | + if err != nil { |
| 156 | + return xerrors.Errorf("failed to parse user ID as int64: %v", err) |
| 157 | + } |
| 158 | + userID := strconv.FormatInt(userInt, 10) |
| 159 | + if !strings.EqualFold(userID, slack.Identity) { |
| 160 | + return xerrors.Errorf("slack userID mismatch: expect %s - actual %s", slack.Identity, userID) |
| 161 | + } |
| 162 | + |
| 163 | + slack.Text = foundMsg.Text |
| 164 | + slack.AltID = userID |
| 165 | + slack.Identity = userID |
| 166 | + |
| 167 | + return slack.validateText() |
| 168 | +} |
| 169 | + |
| 170 | +func (slack *Slack) validateText() (err error) { |
| 171 | + scanner := bufio.NewScanner(strings.NewReader(slack.Text)) |
| 172 | + for scanner.Scan() { |
| 173 | + matched := re.FindStringSubmatch(scanner.Text()) |
| 174 | + if len(matched) < 2 { |
| 175 | + continue // Search for next line |
| 176 | + } |
| 177 | + |
| 178 | + sigBase64 := matched[1] |
| 179 | + sigBytes, err := util.DecodeString(sigBase64) |
| 180 | + if err != nil { |
| 181 | + return xerrors.Errorf("Error when decoding signature %s: %s", sigBase64, err.Error()) |
| 182 | + } |
| 183 | + slack.Signature = sigBytes |
| 184 | + return mycrypto.ValidatePersonalSignature(slack.SignaturePayload, sigBytes, slack.Pubkey) |
| 185 | + } |
| 186 | + return xerrors.Errorf("Signature not found in the slack message.") |
| 187 | +} |
| 188 | + |
| 189 | +func initClient() { |
| 190 | + if client != nil { |
| 191 | + return |
| 192 | + } |
| 193 | + client = slack.New(config.C.Platform.Slack.ApiToken) |
| 194 | + if _, err := client.AuthTest(); err != nil { |
| 195 | + panic(fmt.Errorf("failed to authenticate the slack: %v", err)) |
| 196 | + } |
| 197 | +} |
0 commit comments