-
-
Notifications
You must be signed in to change notification settings - Fork 385
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(server): add apikey auth method
- Loading branch information
Showing
3 changed files
with
38 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package auth | ||
|
||
import "net/http" | ||
|
||
type apiKeyMethod struct { | ||
apiKey string | ||
} | ||
|
||
func newAPIKeyMethod(apiKey string) *apiKeyMethod { | ||
return &apiKeyMethod{ | ||
apiKey: apiKey, | ||
} | ||
} | ||
|
||
// equal returns true if another auth checker is equal. | ||
// This is used to deduplicate checkers for a particular route. | ||
func (a *apiKeyMethod) equal(other authorizationChecker) bool { | ||
otherTokenMethod, ok := other.(*apiKeyMethod) | ||
if !ok { | ||
return false | ||
} | ||
return a.apiKey == otherTokenMethod.apiKey | ||
} | ||
|
||
func (a *apiKeyMethod) isAuthorized(_ http.ResponseWriter, request *http.Request) bool { | ||
xAPIKey := request.Header.Get("X-API-Key") | ||
if xAPIKey == "" { | ||
xAPIKey = request.URL.Query().Get("api_key") | ||
} | ||
return xAPIKey == a.apiKey | ||
} |
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