func init() { auth.AddMechanism("PLAIN", func(e *auth.AuthElement, stream stream.ServerStream) error { var auth_state *auth.AuthState if err := stream.State().Get(&auth_state); err != nil { return err } b, err := auth.DecodeBase64(e.Data, stream) if err != nil { return err } user_password := bytes.Split(b, usernamePasswordSeparator) if pass := auth_state.GetPasswordByUserName(string(user_password[1])); pass == string(user_password[2]) { if err := stream.WriteElement(mechanisms.SuccessElement{}); err != nil { return err } auth_state.UserName = string(user_password[1]) auth_state.Mechanism = "PLAIN" stream.ReOpen() return nil } else { return errors.New("AUTH FAILED") } }) }
func (h *digestMD5Handler) Handle() error { var auth_state *auth.AuthState if err := h.strm.State().Get(&auth_state); err != nil { auth_state = &auth.AuthState{} h.strm.State().Push(auth_state) } if err := h.strm.WriteElement(mechanisms.NewChallengeElement(h.md5.Challenge())); err != nil { return err } // Receive a response with encoded MD5 resp_el, err := mechanisms.ReadResponse(h.strm) if err != nil { return err } // Check MD5 raw_resp_data, err := auth.DecodeBase64(resp_el.Data, h.strm) if err != nil { return err } if err := h.md5.ParseResponse(raw_resp_data); err != nil { return err } password := auth_state.GetPasswordByUserName(h.md5.UserName()) if err := h.md5.Validate(password); err != nil { return err } // Send response if err := h.strm.WriteElement(mechanisms.NewChallengeElement(h.md5.Final())); err != nil { return err } rsp, err := mechanisms.ReadResponse(h.strm) if err != nil { return err } if rsp.Data != "" { return errors.New("Wrong response, expected empty response") } if err := h.strm.WriteElement(mechanisms.SuccessElement{}); err != nil { return err } auth_state.UserName = h.md5.AuthID() h.strm.ReOpen() return nil }