forked from payt0nc/fairplay-ksm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
170 lines (145 loc) · 4.09 KB
/
api.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
package main
import (
"crypto/rsa"
"encoding/base64"
"encoding/hex"
"fmt"
"net/http"
"os"
"strings"
"github.com/aviaki/fairplay-ksm/cryptos"
"github.com/aviaki/fairplay-ksm/ksm"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)
type SpcMessage struct {
Spc string `json:"spc" binding:"required"`
AssetID string `json:"assetID"`
}
type ErrorMessage struct {
Status int `json:"status" binding:"required"`
Message string `json:"message" binding:"required"`
}
type CkcResult struct {
Ckc string `json:"ckc" binding:"required"`
}
var FairplayPrivateKey = ReadPriKey()
var FairplayPublicCertification = ReadPublicCert()
var FairplayASk = ReadASk()
func main() {
// Setting Echo
e := echo.New()
e.Use(middleware.Logger())
e.Use(middleware.Recover())
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
AllowOrigins: []string{"*"}, // FIXME: Use your streaming domain.
AllowMethods: []string{http.MethodGet, http.MethodHead, http.MethodPost},
}))
k := &ksm.Ksm{
Pub: ReadPublicCert(),
Pri: ReadPriKey(),
Rck: ksm.RandomContentKey{}, //NOTE: Don't use ramdom key in your application.
Ask: ReadASk(),
}
e.GET("/", func(ctx echo.Context) error {
return ctx.String(http.StatusOK, "OK")
})
e.POST("/fps/license", func(ctx echo.Context) error {
spcMessage := new(SpcMessage)
var playback []byte
var base64EncodingMethod string
contentType := ctx.Request().Header.Get("Content-Type")
if err := ctx.Bind(spcMessage); err != nil {
errorMessage := &ErrorMessage{Status: 400, Message: err.Error()}
fmt.Println(ctx.Request().Header)
fmt.Println(ctx.Request().Body)
return ctx.JSON(http.StatusBadRequest, errorMessage)
}
if strings.Contains(spcMessage.Spc, "-") || strings.Contains(spcMessage.Spc, "_") {
base64EncodingMethod = "URL"
decoded, err := base64.URLEncoding.DecodeString(spcMessage.Spc)
if err != nil {
panic(err)
}
playback = decoded
} else if strings.Contains(spcMessage.Spc, " ") && strings.Contains(spcMessage.Spc, "/") {
base64EncodingMethod = "STD"
decoded, err := base64.StdEncoding.DecodeString(strings.ReplaceAll(spcMessage.Spc, " ", "+"))
if err != nil {
panic(err)
}
playback = decoded
} else {
base64EncodingMethod = "STD"
decoded, err := base64.StdEncoding.DecodeString(spcMessage.Spc)
if err != nil {
panic(err)
}
playback = decoded
}
ckc, err := k.GenCKC(playback)
if err != nil {
panic(err)
}
var result string
switch base64EncodingMethod {
case "URL":
result = base64.URLEncoding.EncodeToString(ckc)
case "STD":
result = base64.StdEncoding.EncodeToString(ckc)
default:
result = base64.StdEncoding.EncodeToString(ckc)
}
fmt.Println(result)
switch contentType {
case "application/json":
return ctx.JSON(200, &CkcResult{Ckc: result})
case "application/x-www-form-urlencoded":
return ctx.Blob(200, "application/x-www-form-urlencoded", []byte("<ckc>"+result+"</ckc>"))
default:
return ctx.Blob(200, "application/x-www-form-urlencoded", []byte("<ckc>"+result+"</ckc>"))
}
})
e.Logger.Fatal(e.Start(":8080"))
}
func envBase64Decode(s string) []byte {
data, err := base64.StdEncoding.DecodeString(s)
if err != nil {
fmt.Println("Env Decode Error: ", err)
return []byte{}
}
return data
}
func ReadPublicCert() *rsa.PublicKey {
pubEnvVar := envBase64Decode(os.Getenv("FAIRPLAY_CERTIFICATION"))
if len(pubEnvVar) == 0 {
panic("Can't not find FAIRPLAY CERTIFICATION")
}
pubCert, err := cryptos.ParsePublicCertification(pubEnvVar)
if err != nil {
panic(err)
}
return pubCert
}
func ReadPriKey() *rsa.PrivateKey {
priEnvVar := envBase64Decode(os.Getenv("FAIRPLAY_PRIVATE_KEY"))
if len(priEnvVar) == 0 {
panic("Can't not find FAIRPLAY PRIVATE KEY")
}
priKey, err := cryptos.DecryptPriKey(priEnvVar, []byte(""))
if err != nil {
panic(err)
}
return priKey
}
func ReadASk() []byte {
askEnvVar := os.Getenv("FAIRPLAY_APPLICATION_SERVICE_KEY")
if len(askEnvVar) == 0 {
askEnvVar = "d87ce7a26081de2e8eb8acef3a6dc179" //Apple provided
}
ask, err := hex.DecodeString(askEnvVar)
if err != nil {
panic(err)
}
return ask
}