| 1 | package slack |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "net/url" |
| 6 | "regexp" |
| 7 | "strconv" |
| 8 | "strings" |
| 9 | |
| 10 | "github.com/nicholas-fedor/shoutrrr/pkg/types" |
| 11 | ) |
| 12 | |
| 13 | // Token is a Slack API token or a Slack webhook token. |
| 14 | type Token struct { |
| 15 | raw string |
| 16 | } |
| 17 | |
| 18 | const webhookBase = "https://hooks.slack.com/services/" |
| 19 | |
| 20 | // Token type identifiers. |
| 21 | const ( |
| 22 | HookTokenIdentifier = "hook" |
| 23 | UserTokenIdentifier = "xoxp" |
| 24 | BotTokenIdentifier = "xoxb" |
| 25 | ) |
| 26 | |
| 27 | // Token length and offset constants. |
| 28 | const ( |
| 29 | MinTokenLength = 3 // Minimum length for a valid token string |
| 30 | TypeIdentifierLength = 4 // Length of the type identifier (e.g., "xoxb", "hook") |
| 31 | TypeIdentifierOffset = 5 // Offset to skip type identifier and separator (e.g., "xoxb:") |
| 32 | Part1Length = 9 // Expected length of part 1 in token |
| 33 | Part2Length = 9 // Expected length of part 2 in token |
| 34 | Part3Length = 24 // Expected length of part 3 in token |
| 35 | ) |
| 36 | |
| 37 | // Token match group indices. |
| 38 | const ( |
| 39 | tokenMatchFull = iota // Full match |
| 40 | tokenMatchType // Type identifier (e.g., "xoxb", "hook") |
| 41 | tokenMatchPart1 // First part of the token |
| 42 | tokenMatchSep1 // First separator |
| 43 | tokenMatchPart2 // Second part of the token |
| 44 | tokenMatchSep2 // Second separator |
| 45 | tokenMatchPart3 // Third part of the token |
| 46 | tokenMatchCount // Total number of match groups |
| 47 | ) |
| 48 | |
| 49 | var tokenPattern = regexp.MustCompile( |
| 50 | `(?:(?P<type>xox.|hook)[-:]|:?)(?P<p1>[A-Z0-9]{` + strconv.Itoa( |
| 51 | Part1Length, |
| 52 | ) + `,})(?P<s1>[-/,])(?P<p2>[A-Z0-9]{` + strconv.Itoa( |
| 53 | Part2Length, |
| 54 | ) + `,})(?P<s2>[-/,])(?P<p3>[A-Za-z0-9]{` + strconv.Itoa( |
| 55 | Part3Length, |
| 56 | ) + `,})`, |
| 57 | ) |
| 58 | |
| 59 | var _ types.ConfigProp = &Token{} |
| 60 | |
| 61 | // Authorization returns the corresponding `Authorization` HTTP header value for the token. |
| 62 | func (t *Token) Authorization() string { |
| 63 | stringBuilder := strings.Builder{} |
| 64 | stringBuilder.WriteString("Bearer ") |
| 65 | stringBuilder.Grow(len(t.raw)) |
| 66 | stringBuilder.WriteString(t.raw[:TypeIdentifierLength]) |
| 67 | stringBuilder.WriteRune('-') |
| 68 | stringBuilder.WriteString(t.raw[TypeIdentifierOffset:]) |
| 69 | |
| 70 | return stringBuilder.String() |
| 71 | } |
| 72 | |
| 73 | // GetPropValue returns the token as a property value, implementing the types.ConfigProp interface. |
| 74 | func (t *Token) GetPropValue() (string, error) { |
| 75 | if t == nil { |
| 76 | return "", nil |
| 77 | } |
| 78 | |
| 79 | return t.raw, nil |
| 80 | } |
| 81 | |
| 82 | // IsAPIToken returns whether the identifier is set to anything else but the webhook identifier (`hook`). |
| 83 | func (t *Token) IsAPIToken() bool { |
| 84 | return t.TypeIdentifier() != HookTokenIdentifier |
| 85 | } |
| 86 | |
| 87 | // SetFromProp sets the token from a property value, implementing the types.ConfigProp interface. |
| 88 | func (t *Token) SetFromProp(propValue string) error { |
| 89 | if len(propValue) < MinTokenLength { |
| 90 | return ErrInvalidToken |
| 91 | } |
| 92 | |
| 93 | match := tokenPattern.FindStringSubmatch(propValue) |
| 94 | if match == nil || len(match) != tokenMatchCount { |
| 95 | return ErrInvalidToken |
| 96 | } |
| 97 | |
| 98 | typeIdentifier := match[tokenMatchType] |
| 99 | if typeIdentifier == "" { |
| 100 | typeIdentifier = HookTokenIdentifier |
| 101 | } |
| 102 | |
| 103 | t.raw = fmt.Sprintf("%s:%s-%s-%s", |
| 104 | typeIdentifier, match[tokenMatchPart1], match[tokenMatchPart2], match[tokenMatchPart3]) |
| 105 | |
| 106 | if match[tokenMatchSep1] != match[tokenMatchSep2] { |
| 107 | return ErrMismatchedTokenSeparators |
| 108 | } |
| 109 | |
| 110 | return nil |
| 111 | } |
| 112 | |
| 113 | // String returns the token in normalized format with dashes (-) as separator. |
| 114 | func (t *Token) String() string { |
| 115 | return t.raw |
| 116 | } |
| 117 | |
| 118 | // TypeIdentifier returns the type identifier of the token. |
| 119 | func (t *Token) TypeIdentifier() string { |
| 120 | return t.raw[:TypeIdentifierLength] |
| 121 | } |
| 122 | |
| 123 | // UserInfo returns a url.Userinfo struct populated from the token. |
| 124 | func (t *Token) UserInfo() *url.Userinfo { |
| 125 | return url.UserPassword(t.raw[:TypeIdentifierLength], t.raw[TypeIdentifierOffset:]) |
| 126 | } |
| 127 | |
| 128 | // WebhookURL returns the corresponding Webhook URL for the token. |
| 129 | func (t *Token) WebhookURL() string { |
| 130 | stringBuilder := strings.Builder{} |
| 131 | stringBuilder.WriteString(webhookBase) |
| 132 | stringBuilder.Grow(len(t.raw) - TypeIdentifierOffset) |
| 133 | |
| 134 | for i := TypeIdentifierOffset; i < len(t.raw); i++ { |
| 135 | c := t.raw[i] |
| 136 | if c == '-' { |
| 137 | c = '/' |
| 138 | } |
| 139 | |
| 140 | stringBuilder.WriteByte(c) |
| 141 | } |
| 142 | |
| 143 | return stringBuilder.String() |
| 144 | } |
| 145 | |
| 146 | // ParseToken parses and normalizes a token string. |
| 147 | func ParseToken(str string) (*Token, error) { |
| 148 | token := &Token{} |
| 149 | if err := token.SetFromProp(str); err != nil { |
| 150 | return nil, err |
| 151 | } |
| 152 | |
| 153 | return token, nil |
| 154 | } |