-
Notifications
You must be signed in to change notification settings - Fork 42
/
stream-signing.go
72 lines (65 loc) · 1.91 KB
/
stream-signing.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
package tools
import (
"fmt"
"github.com/golang-jwt/jwt/v4"
"github.com/TUM-Dev/gocast/model"
"strings"
"time"
)
type JWTPlaylistClaims struct {
jwt.RegisteredClaims
UserID uint
Playlist string
Download bool
StreamID string
CourseID string
}
// SetSignedPlaylists adds a signed jwt to all available playlist urls that indicates that the
// user is allowed to consume the playlist. The method assumes that the user has been pre-authorized and doesn't
// check for permissions.
func SetSignedPlaylists(s *model.Stream, user *model.User, allowDownloading bool) error {
var playlists []struct{ Type, Playlist string }
if s.PlaylistUrl != "" {
playlists = append(playlists, struct{ Type, Playlist string }{Type: "COMB", Playlist: s.PlaylistUrl})
}
if s.PlaylistUrlCAM != "" {
playlists = append(playlists, struct{ Type, Playlist string }{Type: "CAM", Playlist: s.PlaylistUrlCAM})
}
if s.PlaylistUrlPRES != "" {
playlists = append(playlists, struct{ Type, Playlist string }{Type: "PRES", Playlist: s.PlaylistUrlPRES})
}
for _, playlist := range playlists {
if strings.Contains(playlist.Playlist, "lrz.de") { // todo: remove after migration from lrz services
continue
}
t := jwt.New(jwt.GetSigningMethod("RS256"))
var userid uint
userid = 0
if user != nil {
userid = user.ID
}
t.Claims = &JWTPlaylistClaims{
RegisteredClaims: jwt.RegisteredClaims{
ExpiresAt: &jwt.NumericDate{Time: time.Now().Add(time.Hour * 7)}, // Token expires in 7 hours
},
UserID: userid,
Playlist: playlist.Playlist,
Download: allowDownloading,
StreamID: fmt.Sprintf("%d", s.ID),
CourseID: fmt.Sprintf("%d", s.CourseID),
}
str, err := t.SignedString(Cfg.GetJWTKey())
if err != nil {
return err
}
switch playlist.Type {
case "CAM":
s.PlaylistUrlCAM += "?jwt=" + str
case "PRES":
s.PlaylistUrlPRES += "?jwt=" + str
case "COMB":
s.PlaylistUrl += "?jwt=" + str
}
}
return nil
}