Exemplo n.º 1
0
func (h *RegistrationHooks) OnBind(sess *server.Session, bind *proto.Bind) error {
	authExtra, err := srvdir_proto.UnpackAuthExtra(sess.Auth())
	if err != nil {
		h.Warn("Authentication data is malformed: %v", err)
		return fmt.Errorf("Authentication data is malformed: %v", err)
	}

	payload, err := json.Marshal(&BindRegRequest{Auth: authExtra, Bind: bind})
	if err != nil {
		h.Error("Failed to build bind registration payload: %v", err)
		return fmt.Errorf("Internal error building registration payload")
	}

	resp, err := http.Post(h.onBindURL, "application/json", bytes.NewBuffer(payload))
	if err != nil {
		h.Error("Error consulting registration service: %v", err)
		return nil
	}

	if resp.Body != nil {
		defer resp.Body.Close()
	}

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		h.Error("Error reading registration service response: %v", err)
		return nil
	}

	switch resp.StatusCode {
	case 400:
		var validationErr struct {
			Message string `json:message`
		}
		err = json.Unmarshal(body, &validationErr)
		if err != nil {
			h.Error("Failed to unmarshal registration service error response: %v", err)
			return nil
		}
		h.Info("Registration service failed request: %v", err)
		return fmt.Errorf("%v", validationErr.Message)

	case 200:
		err = json.Unmarshal(body, bind)
		if err != nil {
			h.Error("Failed to unmarshal registration service response: %v", err)
			return nil
		}

	default:
		h.Error("Registration service returned unhandled status code: %v", resp.StatusCode)
		return nil
	}

	return nil
}
Exemplo n.º 2
0
func extractSession(s *server.Session) (*proto.Auth, *srvdir_proto.AuthExtra, error) {
	var auth proto.Auth
	if err := proto.UnpackInterfaceField(s.Auth(), &auth); err != nil {
		return nil, nil, err
	}

	var authExtra srvdir_proto.AuthExtra
	if err := proto.UnpackInterfaceField(s.Auth().Extra, &authExtra); err != nil {
		return nil, nil, err
	}

	return &auth, &authExtra, nil
}
Exemplo n.º 3
0
func (h *MetricsHooks) OnClose(sess *server.Session) error {
	_, authExtra, err := extractSession(sess)
	if err != nil {
		return err
	}

	err = h.client.AddEvent("CloseSession", &sessionCloseEvent{
		Keen: keen.KeenProperties{
			Timestamp: keen.Timestamp(sess.Start()),
		},
		OS:       authExtra.OS,
		ClientId: sess.Id(),
		User:     authExtra.AuthToken,
		Version:  authExtra.ClientVersion,
		Reason:   "", // XXX
		Duration: time.Since(sess.Start()).Seconds(),
	})

	if err != nil {
		h.Error("Failed to record CloseSession event: %v", err)
	}

	return nil
}