-
-
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(control server authentication): add basic http auth (#2423)
- Loading branch information
Showing
3 changed files
with
52 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,36 @@ | ||
package auth | ||
|
||
import ( | ||
"crypto/sha256" | ||
"crypto/subtle" | ||
"net/http" | ||
) | ||
|
||
type basicAuthMethod struct { | ||
authDigest [32]byte | ||
} | ||
|
||
func newBasicAuthMethod(username, password string) *basicAuthMethod { | ||
return &basicAuthMethod{ | ||
authDigest: sha256.Sum256([]byte(username + password)), | ||
} | ||
} | ||
|
||
// equal returns true if another auth checker is equal. | ||
// This is used to deduplicate checkers for a particular route. | ||
func (a *basicAuthMethod) equal(other authorizationChecker) bool { | ||
otherBasicMethod, ok := other.(*basicAuthMethod) | ||
if !ok { | ||
return false | ||
} | ||
return a.authDigest == otherBasicMethod.authDigest | ||
} | ||
|
||
func (a *basicAuthMethod) isAuthorized(request *http.Request) bool { | ||
username, password, ok := request.BasicAuth() | ||
if !ok { | ||
return false | ||
} | ||
requestAuthDigest := sha256.Sum256([]byte(username + password)) | ||
return subtle.ConstantTimeCompare(a.authDigest[:], requestAuthDigest[:]) == 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