forked from 0xProject/rpc-gateway
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from sygmaprotocol/mmuftic/flag-name-fix
feat: Update authentication
- Loading branch information
Showing
4 changed files
with
81 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package auth | ||
|
||
import ( | ||
"net/http" | ||
) | ||
|
||
func URLTokenAuth(token string) func(next http.Handler) http.Handler { | ||
return func(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
authToken := r.URL.Query().Get("auth_token") | ||
if authToken == "" || authToken != token { | ||
w.WriteHeader(http.StatusUnauthorized) | ||
|
||
return | ||
} | ||
next.ServeHTTP(w, r) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package auth | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
) | ||
|
||
func TestUrlTokenAuth(t *testing.T) { | ||
validToken := "valid_token" | ||
middleware := URLTokenAuth(validToken) | ||
|
||
tests := []struct { | ||
name string | ||
url string | ||
expectedStatus int | ||
}{ | ||
{ | ||
name: "Valid token", | ||
url: "/?auth_token=valid_token", | ||
expectedStatus: http.StatusOK, | ||
}, | ||
{ | ||
name: "Invalid token", | ||
url: "/?auth_token=invalid_token", | ||
expectedStatus: http.StatusUnauthorized, | ||
}, | ||
{ | ||
name: "Missing token", | ||
url: "/", | ||
expectedStatus: http.StatusUnauthorized, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
req, err := http.NewRequest("GET", tt.url, nil) | ||
if err != nil { | ||
t.Fatalf("could not create request: %v", err) | ||
} | ||
|
||
rr := httptest.NewRecorder() | ||
handler := middleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusOK) | ||
})) | ||
|
||
handler.ServeHTTP(rr, req) | ||
|
||
if rr.Code != tt.expectedStatus { | ||
t.Errorf("expected status %v; got %v", tt.expectedStatus, rr.Code) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters