Skip to content

Commit a70326c

Browse files
committed
refactor http service with authentication middleware.
1 parent 9b475d9 commit a70326c

13 files changed

+366
-1024
lines changed

platform/ai-talk.go

+1
Original file line numberDiff line numberDiff line change
@@ -1523,6 +1523,7 @@ func handleAITalkService(ctx context.Context, handler *http.ServeMux) error {
15231523
return errors.Wrapf(err, "parse body")
15241524
}
15251525

1526+
// TODO: why do token Authentication only roomToken is empty?
15261527
// Authenticate by bearer token if no room token
15271528
if roomToken == "" {
15281529
apiSecret := envApiSecret()

platform/callback.go

+9-30
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@ import (
99
"crypto/tls"
1010
"encoding/json"
1111
"fmt"
12-
"github.com/google/uuid"
1312
"io/ioutil"
1413
"net/http"
1514
"strconv"
1615
"strings"
1716
"sync"
1817
"time"
1918

19+
"github.com/google/uuid"
20+
2021
// From ossrs.
2122
"github.com/ossrs/go-oryx-lib/errors"
2223
ohttp "github.com/ossrs/go-oryx-lib/http"
@@ -49,23 +50,9 @@ func NewCallbackWorker() *CallbackWorker {
4950
func (v *CallbackWorker) Handle(ctx context.Context, handler *http.ServeMux) error {
5051
ep := "/terraform/v1/mgmt/hooks/query"
5152
logger.Tf(ctx, "Handle %v", ep)
52-
handler.HandleFunc(ep, func(w http.ResponseWriter, r *http.Request) {
53-
if err := func() error {
54-
var token string
55-
if err := ParseBody(ctx, r.Body, &struct {
56-
Token *string `json:"token"`
57-
All *bool `json:"all"`
58-
}{
59-
Token: &token,
60-
}); err != nil {
61-
return errors.Wrapf(err, "parse body")
62-
}
63-
64-
apiSecret := envApiSecret()
65-
if err := Authenticate(ctx, apiSecret, token, r.Header); err != nil {
66-
return errors.Wrapf(err, "authenticate")
67-
}
6853

54+
handler.Handle(ep, middlewareAuthTokenInBody(ctx, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
55+
if err := func() error {
6956
var config CallbackConfig
7057
if err := config.Load(ctx); err != nil {
7158
return errors.Wrapf(err, "load")
@@ -91,34 +78,26 @@ func (v *CallbackWorker) Handle(ctx context.Context, handler *http.ServeMux) err
9178
Response: res,
9279
CallbackConfig: &config,
9380
})
94-
logger.Tf(ctx, "hooks apply ok, %v, token=%vB", config.String(), len(token))
81+
logger.Tf(ctx, "hooks apply ok, %v", config.String())
9582
return nil
9683
}(); err != nil {
9784
ohttp.WriteError(ctx, w, r, err)
9885
}
99-
})
86+
})))
10087

10188
ep = "/terraform/v1/mgmt/hooks/apply"
10289
logger.Tf(ctx, "Handle %v", ep)
103-
handler.HandleFunc(ep, func(w http.ResponseWriter, r *http.Request) {
90+
handler.Handle(ep, middlewareAuthTokenInBody(ctx, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
10491
if err := func() error {
105-
var token string
10692
var config CallbackConfig
10793
if err := ParseBody(ctx, r.Body, &struct {
108-
Token *string `json:"token"`
10994
*CallbackConfig
11095
}{
111-
Token: &token,
11296
CallbackConfig: &config,
11397
}); err != nil {
11498
return errors.Wrapf(err, "parse body")
11599
}
116100

117-
apiSecret := envApiSecret()
118-
if err := Authenticate(ctx, apiSecret, token, r.Header); err != nil {
119-
return errors.Wrapf(err, "authenticate")
120-
}
121-
122101
if err := rdb.HSet(ctx, SRS_HOOKS, "target", config.Target).Err(); err != nil && err != redis.Nil {
123102
return errors.Wrapf(err, "hset %v target %v", SRS_HOOKS, config.Target)
124103
}
@@ -148,12 +127,12 @@ func (v *CallbackWorker) Handle(ctx context.Context, handler *http.ServeMux) err
148127
}
149128

150129
ohttp.WriteData(ctx, w, r, nil)
151-
logger.Tf(ctx, "hooks apply ok, %v, token=%vB", config.String(), len(token))
130+
logger.Tf(ctx, "hooks apply ok, %v", config.String())
152131
return nil
153132
}(); err != nil {
154133
ohttp.WriteError(ctx, w, r, err)
155134
}
156-
})
135+
})))
157136

158137
ep = "/terraform/v1/mgmt/hooks/example"
159138
logger.Tf(ctx, "Handle %v", ep)

platform/camera-live-stream.go

+18-50
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/ossrs/go-oryx-lib/errors"
2121
ohttp "github.com/ossrs/go-oryx-lib/http"
2222
"github.com/ossrs/go-oryx-lib/logger"
23+
2324
// Use v8 because we use Go 1.16+, while v9 requires Go 1.18+
2425
"github.com/go-redis/redis/v8"
2526
"github.com/google/uuid"
@@ -49,25 +50,19 @@ func (v *CameraWorker) GetTask(platform string) *CameraTask {
4950
func (v *CameraWorker) Handle(ctx context.Context, handler *http.ServeMux) error {
5051
ep := "/terraform/v1/ffmpeg/camera/secret"
5152
logger.Tf(ctx, "Handle %v", ep)
52-
handler.HandleFunc(ep, func(w http.ResponseWriter, r *http.Request) {
53+
handler.Handle(ep, middlewareAuthTokenInBody(ctx, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
5354
if err := func() error {
54-
var token, action string
55+
var action string
5556
var userConf CameraConfigure
5657
if err := ParseBody(ctx, r.Body, &struct {
57-
Token *string `json:"token"`
5858
Action *string `json:"action"`
5959
*CameraConfigure
6060
}{
61-
Token: &token, Action: &action, CameraConfigure: &userConf,
61+
Action: &action, CameraConfigure: &userConf,
6262
}); err != nil {
6363
return errors.Wrapf(err, "parse body")
6464
}
6565

66-
apiSecret := envApiSecret()
67-
if err := Authenticate(ctx, apiSecret, token, r.Header); err != nil {
68-
return errors.Wrapf(err, "authenticate")
69-
}
70-
7166
allowedActions := []string{"update"}
7267
allowedPlatforms := []string{"wx", "bilibili", "kuaishou"}
7368
if action != "" {
@@ -122,7 +117,7 @@ func (v *CameraWorker) Handle(ctx context.Context, handler *http.ServeMux) error
122117
}
123118

124119
ohttp.WriteData(ctx, w, r, nil)
125-
logger.Tf(ctx, "Camera: update secret ok, token=%vB", len(token))
120+
logger.Tf(ctx, "Camera: update secret ok")
126121
return nil
127122
} else {
128123
confObjs := make(map[string]*CameraConfigure)
@@ -139,32 +134,18 @@ func (v *CameraWorker) Handle(ctx context.Context, handler *http.ServeMux) error
139134
}
140135

141136
ohttp.WriteData(ctx, w, r, confObjs)
142-
logger.Tf(ctx, "Camera: query configures ok, token=%vB", len(token))
137+
logger.Tf(ctx, "Camera: query configures ok")
143138
return nil
144139
}
145140
}(); err != nil {
146141
ohttp.WriteError(ctx, w, r, err)
147142
}
148-
})
143+
})))
149144

150145
ep = "/terraform/v1/ffmpeg/camera/streams"
151146
logger.Tf(ctx, "Handle %v", ep)
152-
handler.HandleFunc(ep, func(w http.ResponseWriter, r *http.Request) {
147+
handler.Handle(ep, middlewareAuthTokenInBody(ctx, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
153148
if err := func() error {
154-
var token string
155-
if err := ParseBody(ctx, r.Body, &struct {
156-
Token *string `json:"token"`
157-
}{
158-
Token: &token,
159-
}); err != nil {
160-
return errors.Wrapf(err, "parse body")
161-
}
162-
163-
apiSecret := envApiSecret()
164-
if err := Authenticate(ctx, apiSecret, token, r.Header); err != nil {
165-
return errors.Wrapf(err, "authenticate")
166-
}
167-
168149
res := make([]map[string]interface{}, 0)
169150
if configs, err := rdb.HGetAll(ctx, SRS_CAMERA_CONFIG).Result(); err != nil && err != redis.Nil {
170151
return errors.Wrapf(err, "hgetall %v", SRS_CAMERA_CONFIG)
@@ -209,33 +190,26 @@ func (v *CameraWorker) Handle(ctx context.Context, handler *http.ServeMux) error
209190
})
210191

211192
ohttp.WriteData(ctx, w, r, res)
212-
logger.Tf(ctx, "Camera: Query streams ok, token=%vB", len(token))
193+
logger.Tf(ctx, "Camera: Query streams ok")
213194
return nil
214195
}(); err != nil {
215196
ohttp.WriteError(ctx, w, r, err)
216197
}
217-
})
198+
})))
218199

219200
ep = "/terraform/v1/ffmpeg/camera/stream-url"
220201
logger.Tf(ctx, "Handle %v", ep)
221-
handler.HandleFunc(ep, func(w http.ResponseWriter, r *http.Request) {
202+
handler.Handle(ep, middlewareAuthTokenInBody(ctx, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
222203
if err := func() error {
223-
var token string
224204
var qUrl string
225205
if err := ParseBody(ctx, r.Body, &struct {
226-
Token *string `json:"token"`
227206
StreamURL *string `json:"url"`
228207
}{
229-
Token: &token, StreamURL: &qUrl,
208+
StreamURL: &qUrl,
230209
}); err != nil {
231210
return errors.Wrapf(err, "parse body")
232211
}
233212

234-
apiSecret := envApiSecret()
235-
if err := Authenticate(ctx, apiSecret, token, r.Header); err != nil {
236-
return errors.Wrapf(err, "authenticate")
237-
}
238-
239213
// Parse URL to object.
240214
u, err := RebuildStreamURL(qUrl)
241215
if err != nil {
@@ -274,11 +248,11 @@ func (v *CameraWorker) Handle(ctx context.Context, handler *http.ServeMux) error
274248
}(); err != nil {
275249
ohttp.WriteError(ctx, w, r, err)
276250
}
277-
})
251+
})))
278252

279253
ep = "/terraform/v1/ffmpeg/camera/source"
280254
logger.Tf(ctx, "Handle %v", ep)
281-
handler.HandleFunc(ep, func(w http.ResponseWriter, r *http.Request) {
255+
handler.Handle(ep, middlewareAuthTokenInBody(ctx, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
282256
if err := func() error {
283257
type CameraTempFile struct {
284258
// The file name.
@@ -293,23 +267,17 @@ func (v *CameraWorker) Handle(ctx context.Context, handler *http.ServeMux) error
293267
Type FFprobeSourceType `json:"type"`
294268
}
295269

296-
var token, platform string
270+
var platform string
297271
var streams []*CameraTempFile
298272
if err := ParseBody(ctx, r.Body, &struct {
299-
Token *string `json:"token"`
300273
Platform *string `json:"platform"`
301274
Streams *[]*CameraTempFile `json:"files"`
302275
}{
303-
Token: &token, Platform: &platform, Streams: &streams,
276+
Platform: &platform, Streams: &streams,
304277
}); err != nil {
305278
return errors.Wrapf(err, "parse body")
306279
}
307280

308-
apiSecret := envApiSecret()
309-
if err := Authenticate(ctx, apiSecret, token, r.Header); err != nil {
310-
return errors.Wrapf(err, "authenticate")
311-
}
312-
313281
if len(streams) == 0 {
314282
return errors.New("no files")
315283
}
@@ -469,12 +437,12 @@ func (v *CameraWorker) Handle(ctx context.Context, handler *http.ServeMux) error
469437
}{
470438
Platform: platform, Files: parsedStreams,
471439
})
472-
logger.Tf(ctx, "Camera:: Update ok, token=%vB", len(token))
440+
logger.Tf(ctx, "Camera:: Update ok")
473441
return nil
474442
}(); err != nil {
475443
ohttp.WriteError(ctx, w, r, err)
476444
}
477-
})
445+
})))
478446

479447
return nil
480448
}

0 commit comments

Comments
 (0)