Skip to content

Commit

Permalink
Changes to make auth type an enum
Browse files Browse the repository at this point in the history
Replaced booleans for basic and token based authentication in
the fcaAuthHandler struct with an enum type since they are
mutually exclusive.

https://jira.hyperledger.org/browse/FAB-2598

Change-Id: Ica5dca233e6ea6c22055605a19a3cc927faa142a
Signed-off-by: Anil Ambati <aambati@us.ibm.com>
  • Loading branch information
Anil Ambati committed Mar 13, 2017
1 parent c73ce65 commit e183a88
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
22 changes: 10 additions & 12 deletions lib/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -499,20 +499,19 @@ func (s *Server) initEnrollmentSigner() (err error) {
// Register all endpoint handlers
func (s *Server) registerHandlers() {
s.mux = http.NewServeMux()
s.registerHandler("info", newInfoHandler, false, false)
s.registerHandler("register", newRegisterHandler, false, true)
s.registerHandler("enroll", newEnrollHandler, true, false)
s.registerHandler("reenroll", newReenrollHandler, true, false)
s.registerHandler("revoke", newRevokeHandler, true, false)
s.registerHandler("tcert", newTCertHandler, true, false)
s.registerHandler("info", newInfoHandler, noAuth)
s.registerHandler("register", newRegisterHandler, token)
s.registerHandler("enroll", newEnrollHandler, basic)
s.registerHandler("reenroll", newReenrollHandler, token)
s.registerHandler("revoke", newRevokeHandler, token)
s.registerHandler("tcert", newTCertHandler, token)
}

// Register an endpoint handler
func (s *Server) registerHandler(
path string,
getHandler func(server *Server) (http.Handler, error),
basic bool,
token bool) {
at authType) {

var handler http.Handler

Expand All @@ -522,10 +521,9 @@ func (s *Server) registerHandler(
return
}
handler = &fcaAuthHandler{
server: s,
basic: basic,
token: token,
next: handler,
server: s,
authType: at,
next: handler,
}
s.mux.Handle("/"+path, handler)
// TODO: Remove the following line once all SDKs stop using the prefixed paths
Expand Down
22 changes: 15 additions & 7 deletions lib/serverauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,20 @@ const (
enrollmentIDHdrName = "__eid__"
)

// AuthType is the enum for authentication types: basic and token
type authType int

const (
noAuth authType = iota
basic // basic = 1
token // token = 2
)

// Fabric CA authentication handler
type fcaAuthHandler struct {
server *Server
basic bool
token bool
next http.Handler
server *Server
authType authType
next http.Handler
}

var authError = cerr.NewBadRequest(errors.New("Authorization failure"))
Expand All @@ -55,7 +63,7 @@ func (ah *fcaAuthHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Handle performs authentication
func (ah *fcaAuthHandler) serveHTTP(w http.ResponseWriter, r *http.Request) error {
log.Debugf("Received request\n%s", util.HTTPRequestToString(r))
if !ah.basic && !ah.token {
if ah.authType == noAuth {
// No authentication required
return nil
}
Expand All @@ -66,7 +74,7 @@ func (ah *fcaAuthHandler) serveHTTP(w http.ResponseWriter, r *http.Request) erro
}
user, pwd, ok := r.BasicAuth()
if ok {
if !ah.basic {
if ah.authType != basic {
log.Debugf("Basic auth is not allowed; found %s", authHdr)
return errBasicAuthNotAllowed
}
Expand All @@ -85,7 +93,7 @@ func (ah *fcaAuthHandler) serveHTTP(w http.ResponseWriter, r *http.Request) erro
return nil
}
// Perform token verification
if ah.token {
if ah.authType == token {
// read body
body, err := ioutil.ReadAll(r.Body)
if err != nil {
Expand Down

0 comments on commit e183a88

Please sign in to comment.