-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathtoken_source.go
More file actions
212 lines (180 loc) · 4.62 KB
/
Copy pathtoken_source.go
File metadata and controls
212 lines (180 loc) · 4.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package main
import (
"encoding/base64"
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
"time"
)
type tokenSource string
const (
tokenTypeTeams = "teams"
tokenTypeSkype = "skype"
tokenTypeChatSvcAgg = "chatsvcagg"
)
var runtimeTokenTypes = []string{tokenTypeSkype, tokenTypeChatSvcAgg}
var optionalTokenTypes = []string{tokenTypeTeams}
type resolvedToken struct {
TokenType string
Source tokenSource
Location string
Value string
}
const (
tokenSourceEnv tokenSource = "env"
tokenSourceFile tokenSource = "file"
)
func defaultTokenDir() (string, error) {
homeDir, err := os.UserHomeDir()
if err != nil {
return "", fmt.Errorf("cannot retrieve user home directory")
}
return filepath.Join(homeDir, ".config", "fossteams"), nil
}
func tokenEnvName(tokenType string) string {
return "MS_TEAMS_" + strings.ToUpper(tokenType) + "_TOKEN"
}
func tokenFilePath(tokenDir, tokenType string) string {
return filepath.Join(tokenDir, "token-"+tokenType+".jwt")
}
func resolveToken(tokenType, tokenDir string) (resolvedToken, error) {
envName := tokenEnvName(tokenType)
if tokenDir == "" {
if value := strings.TrimSpace(os.Getenv(envName)); value != "" {
return resolvedToken{
TokenType: tokenType,
Source: tokenSourceEnv,
Location: envName,
Value: value,
}, nil
}
}
if tokenDir == "" {
var err error
tokenDir, err = defaultTokenDir()
if err != nil {
return resolvedToken{}, err
}
}
tokenPath := tokenFilePath(tokenDir, tokenType)
tokenBytes, err := os.ReadFile(tokenPath)
if err != nil {
return resolvedToken{}, fmt.Errorf("unable to read %s: %v", tokenPath, err)
}
value := strings.TrimSpace(string(tokenBytes))
if value == "" {
return resolvedToken{}, fmt.Errorf("%s is empty", tokenPath)
}
return resolvedToken{
TokenType: tokenType,
Source: tokenSourceFile,
Location: tokenPath,
Value: value,
}, nil
}
func applyTokenDirToEnv(tokenDir string) error {
if strings.TrimSpace(tokenDir) == "" {
return nil
}
for _, tokenType := range runtimeTokenTypes {
token, err := resolveToken(tokenType, tokenDir)
if err != nil {
return err
}
if err := os.Setenv(tokenEnvName(tokenType), token.Value); err != nil {
return fmt.Errorf("unable to export %s from %s: %v", tokenEnvName(tokenType), token.Location, err)
}
}
return nil
}
type jwtMetadata struct {
Audience string
Subject string
Principal string
ExpiresAt time.Time
HasExpiry bool
Claims map[string]any
}
func parseJWTMetadata(token string) (jwtMetadata, error) {
segments := strings.Split(strings.TrimSpace(token), ".")
if len(segments) < 2 {
return jwtMetadata{}, fmt.Errorf("token does not look like a JWT")
}
claimsBytes, err := decodeJWTSection(segments[1])
if err != nil {
return jwtMetadata{}, fmt.Errorf("unable to decode JWT claims: %v", err)
}
claims := map[string]any{}
if err := json.Unmarshal(claimsBytes, &claims); err != nil {
return jwtMetadata{}, fmt.Errorf("unable to parse JWT claims JSON: %v", err)
}
meta := jwtMetadata{
Audience: firstStringClaim(claims, "aud"),
Subject: firstStringClaim(claims, "sub"),
Principal: firstStringClaim(claims, "preferred_username", "upn", "unique_name"),
Claims: claims,
}
if expValue, ok := claims["exp"]; ok {
expUnix, ok := numericUnixClaim(expValue)
if !ok {
return jwtMetadata{}, fmt.Errorf("invalid exp claim")
}
meta.ExpiresAt = time.Unix(expUnix, 0)
meta.HasExpiry = true
}
return meta, nil
}
func decodeJWTSection(raw string) ([]byte, error) {
if data, err := base64.RawURLEncoding.DecodeString(raw); err == nil {
return data, nil
}
padded := raw
if rem := len(raw) % 4; rem != 0 {
padded += strings.Repeat("=", 4-rem)
}
return base64.URLEncoding.DecodeString(padded)
}
func firstStringClaim(claims map[string]any, keys ...string) string {
for _, key := range keys {
switch value := claims[key].(type) {
case string:
value = strings.TrimSpace(value)
if value != "" {
return value
}
case []any:
for _, item := range value {
if raw, ok := item.(string); ok && strings.TrimSpace(raw) != "" {
return strings.TrimSpace(raw)
}
}
}
}
return ""
}
func numericUnixClaim(value any) (int64, bool) {
switch typed := value.(type) {
case float64:
return int64(typed), true
case float32:
return int64(typed), true
case int64:
return typed, true
case int:
return int64(typed), true
case json.Number:
parsed, err := typed.Int64()
if err == nil {
return parsed, true
}
case string:
var parsed int64
_, err := fmt.Sscanf(strings.TrimSpace(typed), "%d", &parsed)
if err == nil {
return parsed, true
}
}
return 0, false
}