Move AS registration handling to dedicated function and split the switch/case to avoid unnecessary condition

This commit is contained in:
Brendan Abolivier 2018-11-06 13:31:11 +00:00
parent 3eab106f87
commit bb0da7ba9c
No known key found for this signature in database
GPG key ID: 8EF1500759F70623

View file

@ -535,7 +535,7 @@ func handleRegistrationFlow(
// Add SharedSecret to the list of completed registration stages // Add SharedSecret to the list of completed registration stages
sessions.AddCompletedStage(sessionID, authtypes.LoginTypeSharedSecret) sessions.AddCompletedStage(sessionID, authtypes.LoginTypeSharedSecret)
case "", authtypes.LoginTypeApplicationService: case "":
// Extract the access token from the request, if there's one to extract // Extract the access token from the request, if there's one to extract
// (which we can know by checking whether the error is nil or not). // (which we can know by checking whether the error is nil or not).
accessToken, err := auth.ExtractAccessToken(req) accessToken, err := auth.ExtractAccessToken(req)
@ -544,13 +544,52 @@ func handleRegistrationFlow(
// an AS or the request is made as the first step of a registration // an AS or the request is made as the first step of a registration
// using the User-Interactive Authentication API. This can be determined // using the User-Interactive Authentication API. This can be determined
// by whether the request contains an access token. // by whether the request contains an access token.
if err == nil || r.Auth.Type != "" { if err == nil {
// If the auth type explicitely relates to Application Services but return handleApplicationServiceRegistration(
accessToken, err, req, r, cfg, accountDB, deviceDB,
)
}
case authtypes.LoginTypeApplicationService:
accessToken, err := auth.ExtractAccessToken(req)
return handleApplicationServiceRegistration(
accessToken, err, req, r, cfg, accountDB, deviceDB,
)
case authtypes.LoginTypeDummy:
// there is nothing to do
// Add Dummy to the list of completed registration stages
sessions.AddCompletedStage(sessionID, authtypes.LoginTypeDummy)
default:
return util.JSONResponse{
Code: http.StatusNotImplemented,
JSON: jsonerror.Unknown("unknown/unimplemented auth type"),
}
}
// Check if the user's registration flow has been completed successfully
// A response with current registration flow and remaining available methods
// will be returned if a flow has not been successfully completed yet
return checkAndCompleteFlow(sessions.GetCompletedStages(sessionID),
req, r, sessionID, cfg, accountDB, deviceDB)
}
func handleApplicationServiceRegistration(
accessToken string,
tokenErr error,
req *http.Request,
r registerRequest,
cfg *config.Dendrite,
accountDB *accounts.Database,
deviceDB *devices.Database,
) util.JSONResponse {
// If the auth type explicitly relates to Application Services but
// there's no access token provided, return an error. // there's no access token provided, return an error.
if err != nil { if tokenErr != nil {
return util.JSONResponse{ return util.JSONResponse{
Code: http.StatusUnauthorized, Code: http.StatusUnauthorized,
JSON: jsonerror.MissingToken(err.Error()), JSON: jsonerror.MissingToken(tokenErr.Error()),
} }
} }
@ -572,25 +611,6 @@ func handleRegistrationFlow(
) )
} }
case authtypes.LoginTypeDummy:
// there is nothing to do
// Add Dummy to the list of completed registration stages
sessions.AddCompletedStage(sessionID, authtypes.LoginTypeDummy)
default:
return util.JSONResponse{
Code: http.StatusNotImplemented,
JSON: jsonerror.Unknown("unknown/unimplemented auth type"),
}
}
// Check if the user's registration flow has been completed successfully
// A response with current registration flow and remaining available methods
// will be returned if a flow has not been successfully completed yet
return checkAndCompleteFlow(sessions.GetCompletedStages(sessionID),
req, r, sessionID, cfg, accountDB, deviceDB)
}
// checkAndCompleteFlow checks if a given registration flow is completed given // checkAndCompleteFlow checks if a given registration flow is completed given
// a set of allowed flows. If so, registration is completed, otherwise a // a set of allowed flows. If so, registration is completed, otherwise a
// response with // response with