Debian Codegraph @

pkg/services/chat/slack/slack_token.go

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