-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
392 lines (344 loc) · 10.1 KB
/
main.go
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
package main
import (
"context"
"crypto/rand"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"
"path"
"strconv"
"strings"
"time"
"github.com/coreos/go-oidc/v3/oidc"
"github.com/gin-gonic/gin"
"github.com/golang-jwt/jwt"
"github.com/joho/godotenv"
"golang.org/x/oauth2"
)
type Config struct {
JitsiSecret string `mapstructure:"JITSI_SECRET"`
JitsiURL string `mapstructure:"JITSI_URL"`
JitsiSub string `mapstructure:"JITSI_SUB"`
IssuerBaseURL string `mapstructure:"ISSUER_BASE_URL"`
BaseURL string `mapstructure:"BASE_URL"`
ClientID string `mapstructure:"CLIENT_ID"`
Secret string `mapstructure:"SECRET"`
Prejoin bool `mapstructure:"PREJOIN"`
Deeplink bool `mapstructure:"DEEPLINK"`
NameKey string `mapstructure:"NAME_KEY"`
}
var config Config
type PlayLoad struct {
ID string `json:"sub,omitempty"`
Email string `json:"email,omitempty"`
Name string `json:"-"`
}
func (p *PlayLoad) UnmarshalJSON(data []byte) error {
var aux struct {
ID string `json:"sub,omitempty"`
Email string `json:"email,omitempty"`
}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
p.ID = aux.ID
p.Email = aux.Email
var m map[string]interface{}
if err := json.Unmarshal(data, &m); err != nil {
return err
}
if name, ok := m[config.NameKey].(string); ok {
p.Name = name
}
return nil
}
type UserContext struct {
User struct {
Email string `json:"email"`
Name string `json:"name"`
} `json:"user"`
}
func init() {
if err := godotenv.Load(); err != nil {
log.Println("Found no .env-File, will use Environment Variables")
}
config.JitsiSecret = os.Getenv("JITSI_SECRET")
config.JitsiURL = os.Getenv("JITSI_URL")
config.JitsiSub = os.Getenv("JITSI_SUB")
config.IssuerBaseURL = os.Getenv("ISSUER_BASE_URL")
config.BaseURL = os.Getenv("BASE_URL")
config.ClientID = os.Getenv("CLIENT_ID")
config.Secret = os.Getenv("SECRET")
config.Prejoin, _ = strconv.ParseBool(os.Getenv("PREJOIN"))
config.Prejoin = config.Prejoin || false
config.Deeplink, _ = strconv.ParseBool(os.Getenv("DEEPLINK"))
config.Deeplink = config.Deeplink || true
config.NameKey = os.Getenv("NAME_KEY")
if config.NameKey == "" {
config.NameKey = "name"
}
}
func randString(nByte int) (string, error) {
b := make([]byte, nByte)
if _, err := io.ReadFull(rand.Reader, b); err != nil {
return "", err
}
return base64.RawURLEncoding.EncodeToString(b), nil
}
func main() {
ctx := context.Background()
provider, err := oidc.NewProvider(ctx, config.IssuerBaseURL)
if err != nil {
log.Fatal("Error when trying to connect to OICD Provider", err)
}
oauthConfig := oauth2.Config{
ClientID: config.ClientID,
ClientSecret: config.Secret,
Endpoint: provider.Endpoint(),
RedirectURL: strings.Trim(config.BaseURL, "/") + "/callback",
Scopes: []string{oidc.ScopeOpenID, "profile", "email"},
}
gin.SetMode(gin.ReleaseMode)
r := gin.Default()
r.SetTrustedProxies(nil)
r.GET("/authenticate", func(c *gin.Context) {
jitsiState := c.Query("state")
room := c.Query("room")
var data map[string]interface{}
err := json.Unmarshal([]byte(jitsiState), &data)
if err != nil {
log.Println("Error analyzing JSON from State:", err)
return
}
client := "browser"
if val, ok := data["electron"].(bool); ok && val {
client = "electron"
} else if val, ok := data["ios"].(bool); ok && val {
client = "ios"
} else if val, ok := data["android"].(bool); ok && val {
client = "android"
}
state, err := randString(16)
if err != nil {
c.AbortWithStatus(http.StatusInternalServerError)
return
}
nonce, err := randString(16)
if err != nil {
c.AbortWithStatus(http.StatusInternalServerError)
return
}
stateData := map[string]interface{}{
"originalState": state,
"client": client,
}
stateJSON, err := json.Marshal(stateData)
if err != nil {
c.AbortWithStatus(http.StatusInternalServerError)
return
}
encodedState := base64.RawURLEncoding.EncodeToString(stateJSON)
c.SetCookie("state", encodedState, int(time.Hour.Seconds()), "/", "", c.Request.TLS != nil, true)
c.SetCookie("nonce", nonce, int(time.Hour.Seconds()), "/", "", c.Request.TLS != nil, true)
c.SetCookie("room", room, int(time.Hour.Seconds()), "/", "", c.Request.TLS != nil, true)
c.Redirect(http.StatusFound, oauthConfig.AuthCodeURL(encodedState, oidc.Nonce(nonce)))
})
r.GET("/callback", func(c *gin.Context) {
stateDataEncoded, err := c.Cookie("state")
if err != nil {
c.String(http.StatusInternalServerError, "state not found")
return
}
if c.Query("state") != stateDataEncoded {
c.String(http.StatusInternalServerError, "state did not match")
return
}
c.SetCookie("state", "", -1, "/", "", c.Request.TLS != nil, true)
nonce, err := c.Cookie("nonce")
if err != nil {
c.String(http.StatusInternalServerError, "nonce not found")
return
}
room, err := c.Cookie("room")
if err != nil {
c.String(http.StatusInternalServerError, "state not set")
return
}
c.SetCookie("room", "", -1, "/", "", c.Request.TLS != nil, true)
oauth2Token, err := oauthConfig.Exchange(ctx, c.Query("code"))
if err != nil {
c.String(http.StatusInternalServerError, fmt.Sprintf("Failed to exchange token: %v", err))
return
}
rawIDToken, ok := oauth2Token.Extra("id_token").(string)
if !ok {
c.String(http.StatusInternalServerError, "No id_token field in oauth2 token.")
return
}
oidcConfig := &oidc.Config{
ClientID: config.ClientID,
}
verifier := provider.Verifier(oidcConfig)
idToken, err := verifier.Verify(ctx, rawIDToken)
if err != nil {
c.String(http.StatusInternalServerError, fmt.Sprintf("Failed to verify ID Token: %v", err))
return
}
if idToken.Nonce != nonce {
c.String(http.StatusBadRequest, "nonce did not match")
return
}
c.SetCookie("nonce", "", -1, "/", "", c.Request.TLS != nil, true)
oauth2Token.AccessToken = "*REDACTED*"
resp := struct {
OAuth2Token *oauth2.Token
IDTokenClaims *json.RawMessage // ID Token payload is just JSON.
}{oauth2Token, new(json.RawMessage)}
if err := idToken.Claims(&resp.IDTokenClaims); err != nil {
c.String(http.StatusInternalServerError, err.Error())
return
}
var playLoad PlayLoad
err = json.Unmarshal(*resp.IDTokenClaims, &playLoad)
if err != nil {
c.String(http.StatusInternalServerError, err.Error())
return
}
user := &UserContext{}
user.User.Email = playLoad.Email
user.User.Name = playLoad.Name
stateJSON, err := base64.RawURLEncoding.DecodeString(stateDataEncoded)
if err != nil {
c.String(http.StatusInternalServerError, "failed to decode state")
return
}
var stateData map[string]interface{}
err = json.Unmarshal(stateJSON, &stateData)
if err != nil {
c.String(http.StatusInternalServerError, "failed to parse state")
return
}
claims := jwt.MapClaims{}
claims["exp"] = time.Now().Add(time.Hour * 24 * 30).Unix()
claims["aud"] = "jitsi"
claims["sub"] = config.JitsiSub
claims["iss"] = "jitsi"
claims["room"] = url.PathEscape(room)
claims["context"] = user
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
tokenString, err := token.SignedString([]byte(config.JitsiSecret))
if err != nil {
c.String(http.StatusInternalServerError, err.Error())
return
}
jitsiURL, err := url.Parse(config.JitsiURL)
if err != nil {
c.String(http.StatusInternalServerError, fmt.Sprintf("Invalid Jitsi URL: %v", err))
return
}
jitsiURL.Path = path.Join(jitsiURL.Path, room)
q := jitsiURL.Query()
q.Set("jwt", tokenString)
jitsiURL.RawQuery = q.Encode()
if !config.Prejoin {
jitsiURL.Fragment = "config.prejoinConfig.enabled=false"
}
originalURL := jitsiURL.String()
client := stateData["client"].(string)
if config.Deeplink {
switch client {
case "electron":
jitsiURL.Scheme = "jitsi-meet"
case "ios", "android":
jitsiURL.Scheme = "org.jitsi.meet"
}
}
accept := c.GetHeader("Accept-Language")
var lang string
if strings.Contains(accept, "de") {
lang = "de"
} else {
lang = "en"
}
var title, redirectMsg, closeMsg, openMsg, buttonText string
if lang == "de" {
title = "Weiterleitung zu Jitsi Desktop"
redirectMsg = "Sie werden zur Jitsi Desktop Applikation weitergeleitet"
closeMsg = "Bitte schließen Sie anschließend dieses Fenster manuell."
openMsg = "Nur falls sich die App nicht öffnet:"
buttonText = "Im Browser öffnen"
} else {
title = "Redirecting to Jitsi Desktop"
redirectMsg = "You are being redirected to the Jitsi Desktop Application"
closeMsg = "Please close this window manually afterwards."
openMsg = "Only if the app does not open:"
buttonText = "Open in Browser"
}
if client == "electron" || client == "ios" || client == "android" {
c.Header("Content-Type", "text/html; charset=utf-8")
c.String(http.StatusOK, fmt.Sprintf(`
<html lang="%s">
<head>
<title>%s</title>
<!-- ... -->
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
text-align: center;
}
.container {
max-width: 500px;
margin: 50px auto;
background-color: #fff;
padding: 30px;
border-radius: 5px;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}
h3 {
color: #333;
}
p {
color: #666;
margin-bottom: 20px;
}
.button {
display: inline-block;
padding: 10px 20px;
background-color: #007BFF;
color: #fff;
text-decoration: none;
border-radius: 5px;
}
</style>
</head>
<body>
<div class="container">
<h3>%s</h3>
<p>%s</p>
<p>%s</p>
<a href="%s" class="button">%s</a>
</div>
<script>
setTimeout(function() {
window.location.href = "%s";
}, 100);
</script>
</body>
</html>
`, lang, title, redirectMsg, closeMsg, openMsg, originalURL, buttonText, jitsiURL.String()))
} else {
c.Redirect(http.StatusFound, jitsiURL.String())
}
})
log.Println("Jitsi OpenID Server started on port 3001")
server_err := r.Run(":3001")
if server_err != nil {
log.Println("Error starting Jitsi OpenID server:", server_err)
}
}