|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +package authentication |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "fmt" |
| 8 | + "log" |
| 9 | + "net/http" |
| 10 | + "net/http/httptest" |
| 11 | + "testing" |
| 12 | + "time" |
| 13 | + |
| 14 | + "github.com/stretchr/testify/assert" |
| 15 | + "github.com/twitchdev/twitch-cli/internal/database" |
| 16 | + "github.com/twitchdev/twitch-cli/internal/util" |
| 17 | + "github.com/twitchdev/twitch-cli/test_setup" |
| 18 | +) |
| 19 | + |
| 20 | +var a *assert.Assertions |
| 21 | +var ac = database.AuthenticationClient{ID: "1234", Secret: "1234", Name: "test_client", IsExtension: false} |
| 22 | +var token = "potato" |
| 23 | +var firstRun = true |
| 24 | + |
| 25 | +func TestHasScope(t *testing.T) { |
| 26 | + a = test_setup.SetupTestEnv(t) |
| 27 | + |
| 28 | + u := UserAuthentication{UserID: "1", Scopes: []string{"user:read:email"}} |
| 29 | + |
| 30 | + a.Equal(true, u.HasScope("user:read:email")) |
| 31 | + a.Equal(false, u.HasScope("user:read")) |
| 32 | + |
| 33 | + a.Equal(true, u.HasOneOfRequiredScope([]string{})) |
| 34 | + a.Equal(true, u.HasOneOfRequiredScope([]string{"user:read:email", "user:read"})) |
| 35 | + a.Equal(false, u.HasOneOfRequiredScope([]string{"user:read"})) |
| 36 | + |
| 37 | +} |
| 38 | + |
| 39 | +func TestNatchesBroadcasterIDParam(t *testing.T) { |
| 40 | + a = test_setup.SetupTestEnv(t) |
| 41 | + |
| 42 | + req, _ := http.NewRequest(http.MethodGet, "http://google.com", nil) |
| 43 | + u := UserAuthentication{UserID: "1", Scopes: []string{"user:read:email"}} |
| 44 | + |
| 45 | + q := req.URL.Query() |
| 46 | + q.Set("broadcaster_id", "2") |
| 47 | + req.URL.RawQuery = q.Encode() |
| 48 | + |
| 49 | + a.Equal(false, u.MatchesBroadcasterIDParam(req)) |
| 50 | + |
| 51 | + q.Set("broadcaster_id", "1") |
| 52 | + req.URL.RawQuery = q.Encode() |
| 53 | + |
| 54 | + a.Equal(true, u.MatchesBroadcasterIDParam(req)) |
| 55 | +} |
| 56 | + |
| 57 | +func TestAuthenticationMiddleware(t *testing.T) { |
| 58 | + a = test_setup.SetupTestEnv(t) |
| 59 | + ts := httptest.NewServer(baseMiddleware(AuthenticationMiddleware(testEndpoint{}))) |
| 60 | + |
| 61 | + req, _ := http.NewRequest(http.MethodGet, ts.URL+testEndpoint{}.Path(), nil) |
| 62 | + |
| 63 | + resp, err := http.DefaultClient.Do(req) |
| 64 | + a.Nil(err) |
| 65 | + a.Equal(401, resp.StatusCode) |
| 66 | + |
| 67 | + resp, err = http.DefaultClient.Do(req) |
| 68 | + a.Nil(err) |
| 69 | + a.Equal(401, resp.StatusCode) |
| 70 | + |
| 71 | + req.Header.Set("Client-ID", ac.ID) |
| 72 | + resp, err = http.DefaultClient.Do(req) |
| 73 | + a.Nil(err) |
| 74 | + a.Equal(401, resp.StatusCode) |
| 75 | + |
| 76 | + req.Header.Set("Authorization", fmt.Sprintf("Bearer %v", token)) |
| 77 | + resp, err = http.DefaultClient.Do(req) |
| 78 | + a.Nil(err) |
| 79 | + a.Equal(200, resp.StatusCode) |
| 80 | + |
| 81 | + req.Header.Set("Authorization", fmt.Sprintf("Bearer%v", token)) |
| 82 | + resp, err = http.DefaultClient.Do(req) |
| 83 | + a.Nil(err) |
| 84 | + a.Equal(401, resp.StatusCode) |
| 85 | + |
| 86 | + req.Header.Set("Authorization", fmt.Sprintf("Bearer")) |
| 87 | + resp, err = http.DefaultClient.Do(req) |
| 88 | + a.Nil(err) |
| 89 | + a.Equal(401, resp.StatusCode) |
| 90 | +} |
| 91 | + |
| 92 | +func baseMiddleware(next http.Handler) http.Handler { |
| 93 | + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 94 | + ctx := context.Background() |
| 95 | + |
| 96 | + // just stub it all |
| 97 | + db, err := database.NewConnection() |
| 98 | + if err != nil { |
| 99 | + log.Fatalf("Error connecting to database: %v", err.Error()) |
| 100 | + return |
| 101 | + } |
| 102 | + if firstRun == true { |
| 103 | + ac, err = db.NewQuery(r, 100).InsertOrUpdateAuthenticationClient(ac, false) |
| 104 | + a.Nil(err) |
| 105 | + auth, err := db.NewQuery(r, 100).CreateAuthorization(database.Authorization{ClientID: ac.ID, UserID: "1", Scopes: "user:read:email bits:read", Token: token, ExpiresAt: util.GetTimestamp().Add(7 * 24 * time.Hour).Format(time.RFC3339)}) |
| 106 | + token = auth.Token |
| 107 | + a.Nil(err) |
| 108 | + |
| 109 | + firstRun = false |
| 110 | + } |
| 111 | + |
| 112 | + defer db.DB.Close() |
| 113 | + |
| 114 | + ctx = context.WithValue(ctx, "db", db) |
| 115 | + r = r.WithContext(ctx) |
| 116 | + |
| 117 | + next.ServeHTTP(w, r) |
| 118 | + }) |
| 119 | +} |
| 120 | + |
| 121 | +type testEndpoint struct{} |
| 122 | + |
| 123 | +func (e testEndpoint) Path() string { return "/endpoint" } |
| 124 | + |
| 125 | +func (e testEndpoint) GetRequiredScopes(method string) []string { |
| 126 | + return []string{} |
| 127 | +} |
| 128 | + |
| 129 | +func (e testEndpoint) ValidMethod(method string) bool { |
| 130 | + return true |
| 131 | +} |
| 132 | + |
| 133 | +func (e testEndpoint) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| 134 | + userCtx := r.Context().Value("auth").(UserAuthentication) |
| 135 | + |
| 136 | + a.NotNil(userCtx) |
| 137 | + w.WriteHeader(200) |
| 138 | +} |
0 commit comments