Use descriptive variable names, make comments clear

Signed-off-by: Anant Prakash <anantprakashjsr@gmail.com>
This commit is contained in:
Anant Prakash 2018-03-13 19:25:07 +05:30
parent a304a8b67e
commit 369fb26a14
No known key found for this signature in database
GPG key ID: C5D399F626523045

View file

@ -50,26 +50,36 @@ const (
sessionIDLength = 24 sessionIDLength = 24
) )
// sessionsDict represents every sessions' completed flow stages.
type sessionsDict struct { type sessionsDict struct {
sessions map[string][]authtypes.LoginType sessions map[string][]authtypes.LoginType
} }
func (d sessionsDict) Get(key string) []authtypes.LoginType { // GetCompletedStages returns the completed stages for a session.
if v, ok := d.sessions[key]; ok { func (d sessionsDict) GetCompletedStages(sessionID string) []authtypes.LoginType {
return v if completedStages, ok := d.sessions[sessionID]; ok {
return completedStages
} }
// Ensure that a empty slice is return and not nil. See gh #399.
return make([]authtypes.LoginType, 0) return make([]authtypes.LoginType, 0)
} }
func (d *sessionsDict) AddCompletedStage(key string, v authtypes.LoginType) { // AddCompletedStage adds a completed stage to the session.
d.sessions[key] = append(d.Get(key), v) func (d *sessionsDict) AddCompletedStage(sessionID string, stage authtypes.LoginType) {
d.sessions[sessionID] = append(d.GetCompletedStages(sessionID), stage)
}
// newSessionsDict returns a sessionsDict whose contained map is initialized and empty.
func newSessionsDict() *sessionsDict {
return &sessionsDict{
sessions: make(map[string][]authtypes.LoginType),
}
} }
var ( var (
// TODO: Remove old sessions. Need to do so on a session-specific timeout. // TODO: Remove old sessions. Need to do so on a session-specific timeout.
sessions = sessionsDict{ // Sessions and completed flow stages // sessions stores the completed flow stages for all sessions. Referenced using their sessionID.
sessions: make(map[string][]authtypes.LoginType), sessions = newSessionsDict()
}
validUsernameRegex = regexp.MustCompile(`^[0-9a-z_\-./]+$`) validUsernameRegex = regexp.MustCompile(`^[0-9a-z_\-./]+$`)
) )
@ -129,7 +139,7 @@ func newUserInteractiveResponse(
params map[string]interface{}, params map[string]interface{},
) userInteractiveResponse { ) userInteractiveResponse {
return userInteractiveResponse{ return userInteractiveResponse{
fs, sessions.Get(sessionID), params, sessionID, fs, sessions.GetCompletedStages(sessionID), params, sessionID,
} }
} }
@ -495,7 +505,8 @@ func handleRegistrationFlow(
// Check if the user's registration flow has been completed successfully // Check if the user's registration flow has been completed successfully
// A response with current registration flow and remaining available methods // A response with current registration flow and remaining available methods
// will be returned if a flow has not been successfully completed yet // will be returned if a flow has not been successfully completed yet
return checkAndCompleteFlow(sessions.Get(sessionID), req, r, sessionID, cfg, accountDB, deviceDB) 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