-
-
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 (#2437)
- Loading branch information
Showing
4 changed files
with
60 additions
and
4 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,34 @@ | ||
package auth | ||
|
||
import ( | ||
"crypto/subtle" | ||
"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(request *http.Request) bool { | ||
xAPIKey := request.Header.Get("X-API-Key") | ||
if xAPIKey == "" { | ||
xAPIKey = request.URL.Query().Get("api_key") | ||
} | ||
return subtle.ConstantTimeCompare([]byte(xAPIKey), []byte(a.apiKey)) == 1 | ||
} |
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
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