Skip to content

Commit

Permalink
use constant time when authenticating users
Browse files Browse the repository at this point in the history
[#161248339]

Signed-off-by: Jack Newberry <[email protected]>
Co-authored-by: Jack Newberry <[email protected]>
  • Loading branch information
Derik Evangelista and jacknewberry committed Oct 16, 2018
1 parent 036a459 commit c37f78a
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,21 @@

package auth

import "net/http"
import (
"crypto/sha256"
"crypto/subtle"
"net/http"
)

type Wrapper struct {
username string
password string
username []byte
password []byte
}

func NewWrapper(username, password string) *Wrapper {
return &Wrapper{
username: username,
password: password,
}
u := sha256.Sum256([]byte(username))
p := sha256.Sum256([]byte(password))
return &Wrapper{username: u[:], password: p[:]}
}

const notAuthorized = "Not Authorized"
Expand Down Expand Up @@ -55,5 +58,9 @@ func (wrapper *Wrapper) WrapFunc(handlerFunc http.HandlerFunc) http.HandlerFunc

func authorized(wrapper *Wrapper, r *http.Request) bool {
username, password, isOk := r.BasicAuth()
return isOk && username == wrapper.username && password == wrapper.password
u := sha256.Sum256([]byte(username))
p := sha256.Sum256([]byte(password))
return isOk &&
subtle.ConstantTimeCompare(wrapper.username, u[:]) == 1 &&
subtle.ConstantTimeCompare(wrapper.password, p[:]) == 1
}

0 comments on commit c37f78a

Please sign in to comment.