Пример #1
0
func (p *DeleteContactRequestHandler) ResourceExists(req wm.Request, cxt wm.Context) (bool, wm.Request, wm.Context, int, error) {
	dcc := cxt.(DeleteContactContext)
	path := req.URLParts()
	pathLen := len(path)
	if path[pathLen-1] == "" {
		// ignore trailing slash
		pathLen = pathLen - 1
	}
	contactId := ""
	if pathLen == 9 {
		contactId = path[8]
	} else if pathLen == 6 {
		contactId = path[5]
	}
	dcc.SetContactId(contactId)
	if contactId == "" || dcc.User() == nil || dcc.User().Id == "" {
		return false, req, cxt, 0, nil
	}
	contact, _, err := p.contactsDS.RetrieveDsocialContact(dcc.User().Id, contactId)
	dcc.SetContact(contact)
	if contact != nil {
		dcc.SetETag(contact.Etag)
		if contact.ModifiedAt > 0 {
			dcc.SetLastModified(time.Unix(contact.ModifiedAt, 0).UTC())
		}
	} else {
		dcc.SetETag("")
		dcc.SetLastModified(time.Time{})
	}
	httpStatus := 0
	if err != nil {
		httpStatus = http.StatusInternalServerError
	}
	return contact != nil, req, cxt, httpStatus, err
}
Пример #2
0
func (p *LoginAccountRequestHandler) ContentTypesAccepted(req wm.Request, cxt wm.Context) ([]wm.MediaTypeInputHandler, wm.Request, wm.Context, int, error) {
	arr := []wm.MediaTypeInputHandler{
		apiutil.NewJSONMediaTypeInputHandler("", "", p, req.Body()),
		apiutil.NewUrlEncodedMediaTypeInputHandler("", "", p),
	}
	return arr, req, cxt, 0, nil
}
Пример #3
0
func (p *UpdateContactRequestHandler) StartRequest(req wm.Request, cxt wm.Context) (wm.Request, wm.Context) {
	ucc := p.GenerateContext(req, cxt)
	path := req.URLParts()
	pathLen := len(path)
	if path[pathLen-1] == "" {
		// ignore trailing slash
		pathLen = pathLen - 1
	}
	var userId string
	var contactId string
	switch pathLen {
	case 9:
		userId = path[5]
		contactId = path[8]
	case 6:
		userId = path[2]
		contactId = path[5]
	}
	if userId != "" {
		user, _ := p.ds.RetrieveUserAccountById(userId)
		ucc.SetUser(user)
		if contactId != "" {
			contact, _, _ := p.contactsDS.RetrieveDsocialContact(userId, contactId)
			ucc.SetOriginalContact(contact)
		}
	}
	return req, ucc
}
Пример #4
0
func (p *ViewAccountRequestHandler) StartRequest(req wm.Request, cxt wm.Context) (wm.Request, wm.Context) {
	vac := p.GenerateContext(req, cxt)
	path := req.URLParts()
	pathLen := len(path)
	if pathLen >= 8 {
		vac.SetType(path[5])
		var id string
		if path[pathLen-1] == "" {
			id = strings.Join(path[7:pathLen-1], "/")
		} else {
			id = strings.Join(path[7:], "/")
		}
		switch vac.Type() {
		case "user":
			user, _ := p.ds.RetrieveUserAccountById(id)
			vac.SetUser(user)
		case "consumer":
			consumer, _ := p.ds.RetrieveConsumerAccountById(id)
			vac.SetConsumer(consumer)
		case "external_user":
			externalUser, _ := p.ds.RetrieveExternalUserAccountById(id)
			vac.SetExternalUser(externalUser)
		}
	}
	return req, vac
}
Пример #5
0
func (p *CreateAccountRequestHandler) StartRequest(req wm.Request, cxt wm.Context) (wm.Request, wm.Context) {
	cac := p.GenerateContext(req, cxt)
	path := req.URLParts()
	if len(path) >= 6 {
		cac.SetType(path[5])
	}
	return req, cac
}
Пример #6
0
func (p *UpdateContactRequestHandler) IsAuthorized(req wm.Request, cxt wm.Context) (bool, string, wm.Request, wm.Context, int, error) {
	ucc := cxt.(UpdateContactContext)
	hasSignature, userId, _, err := apiutil.CheckSignature(p.authDS, req.UnderlyingRequest())
	if !hasSignature || err != nil {
		return hasSignature, "dsocial", req, cxt, http.StatusUnauthorized, err
	}
	if userId != "" {
		user, _ := p.ds.RetrieveUserAccountById(userId)
		ucc.SetAuthUser(user)
	}
	return userId != "", "", req, cxt, 0, nil
}
Пример #7
0
func (p *LogoutAccountRequestHandler) IsAuthorized(req wm.Request, cxt wm.Context) (bool, string, wm.Request, wm.Context, int, error) {
	lac := cxt.(LogoutAccountContext)
	hasSignature, userId, _, err := apiutil.CheckSignature(p.authDS, req.UnderlyingRequest())
	if !hasSignature || err != nil {
		return hasSignature, "dsocial", req, cxt, http.StatusUnauthorized, err
	}
	accessKey, _ := apiutil.RetrieveAccessKeyFromRequest(p.authDS, req.UnderlyingRequest())
	lac.SetAccessKey(accessKey)
	if userId != "" {
		user, _ := p.ds.RetrieveUserAccountById(userId)
		lac.SetUser(user)
	}
	return true, "", req, cxt, 0, nil
}
Пример #8
0
func (p *ViewAccountRequestHandler) IsAuthorized(req wm.Request, cxt wm.Context) (bool, string, wm.Request, wm.Context, int, error) {
	vac := cxt.(ViewAccountContext)
	hasSignature, userId, consumerId, err := apiutil.CheckSignature(p.authDS, req.UnderlyingRequest())
	if !hasSignature || err != nil {
		return hasSignature, "dsocial", req, cxt, http.StatusUnauthorized, err
	}
	if userId != "" {
		user, _ := p.ds.RetrieveUserAccountById(userId)
		vac.SetRequestingUser(user)
	}
	if consumerId != "" {
		consumer, _ := p.ds.RetrieveConsumerAccountById(consumerId)
		vac.SetRequestingConsumer(consumer)
	}
	return true, "", req, cxt, 0, nil
}
Пример #9
0
func (p *ViewAccountRequestHandler) HandlerFor(req wm.Request, writer wm.ResponseWriter) wm.RequestHandler {
	// /api/v1/json/account/(user|consumer|external_user)/view/(id)
	path := req.URLParts()
	pathLen := len(path)
	if path[pathLen-1] == "" {
		// ignore trailing slash
		pathLen = pathLen - 1
	}
	if pathLen >= 8 {
		if path[0] == "" && path[1] == "api" && path[2] == "v1" && path[3] == "json" && path[4] == "account" && path[6] == "view" {
			switch path[5] {
			case "user", "consumer", "external_user":
				return p
			}
		}
	}
	return nil
}
Пример #10
0
func UserIdFromRequestUrl(req wm.Request) string {
    path := req.URLParts()
    pathLen := len(path)
    if path[pathLen-1] == "" {
        // ignore trailing slash
        pathLen = pathLen - 1
    }
    if pathLen >= 6 {
        if path[0] == "" && path[1] == "api" && path[2] == "v1" && path[3] == "json" && path[4] == "u" {
            return path[5]
        }
    }
    if pathLen >= 3 {
        if path[0] == "" && path[1] == "u" {
            return path[2]
        }
    }
    return ""
}
Пример #11
0
func (p *UpdateContactRequestHandler) HandlerFor(req wm.Request, writer wm.ResponseWriter) wm.RequestHandler {
	// /api/v1/json/account/(user|consumer|external_user)/update/(id)
	path := req.URLParts()
	pathLen := len(path)
	if path[pathLen-1] == "" {
		// ignore trailing slash
		pathLen = pathLen - 1
	}
	if pathLen == 9 {
		if path[0] == "" && path[1] == "api" && path[2] == "v1" && path[3] == "json" && path[4] == "u" && path[5] != "" && path[6] == "contacts" && path[7] == "update" && path[8] != "" {
			return p
		}
	} else if pathLen == 6 {
		if path[0] == "" && path[1] == "u" && path[2] != "" && path[3] == "contacts" && path[4] == "update" && path[5] != "" {
			return p
		}
	}
	return nil
}
Пример #12
0
func (p *UpdateAccountRequestHandler) StartRequest(req wm.Request, cxt wm.Context) (wm.Request, wm.Context) {
	uac := p.GenerateContext(req, cxt)
	path := req.URLParts()
	pathLen := len(path)
	if pathLen >= 8 {
		uac.SetType(path[5])
		var id string
		if path[pathLen-1] == "" {
			id = strings.Join(path[7:pathLen-1], "/")
		} else {
			id = strings.Join(path[7:], "/")
		}
		switch uac.Type() {
		case "user":
			user, _ := p.ds.RetrieveUserAccountById(id)
			uac.SetUser(user)
			if user == nil {
				//log.Printf("[UARH]: Setting original value for user: %#v\n", nil)
				uac.SetOriginalValue(nil)
			} else {
				//log.Printf("[UARH]: Setting original value for user: %#v\n", user)
				uac.SetOriginalValue(user)
			}
		case "consumer":
			consumer, _ := p.ds.RetrieveConsumerAccountById(id)
			uac.SetConsumer(consumer)
			if consumer == nil {
				uac.SetOriginalValue(nil)
			} else {
				uac.SetOriginalValue(consumer)
			}
		case "external_user":
			externalUser, _ := p.ds.RetrieveExternalUserAccountById(id)
			uac.SetExternalUser(externalUser)
			if externalUser == nil {
				uac.SetOriginalValue(nil)
			} else {
				uac.SetOriginalValue(externalUser)
			}
		}
	}
	return req, uac
}
Пример #13
0
func (p *GeneratePrivateKeyRequestHandler) IsAuthorized(req wm.Request, cxt wm.Context) (bool, string, wm.Request, wm.Context, int, error) {
	gpkc := cxt.(GeneratePrivateKeyContext)
	hasSignature, userId, consumerId, err := apiutil.CheckSignature(p.authDS, req.UnderlyingRequest())
	if !hasSignature || err != nil {
		return hasSignature, "dsocial", req, cxt, http.StatusUnauthorized, err
	}
	if userId != "" {
		user, _ := p.ds.RetrieveUserAccountById(userId)
		gpkc.SetUser(user)
	}
	if consumerId != "" {
		consumer, _ := p.ds.RetrieveConsumerAccountById(consumerId)
		gpkc.SetConsumer(consumer)
	}
	if (userId != "" && gpkc.User() == nil) || (consumerId != "" && gpkc.Consumer() == nil) {
		gpkc.SetUser(nil)
		gpkc.SetConsumer(nil)
	}
	return true, "", req, cxt, 0, nil
}
Пример #14
0
func (p *GeneratePrivateKeyRequestHandler) HandlerFor(req wm.Request, writer wm.ResponseWriter) wm.RequestHandler {
	// /api/v1/json/auth/login
	// /auth/login
	path := req.URLParts()
	pathLen := len(path)
	if path[pathLen-1] == "" {
		// ignore trailing slash
		pathLen = pathLen - 1
	}
	if pathLen == 6 {
		if path[0] == "" && path[1] == "api" && path[2] == "v1" && path[3] == "json" && path[4] == "auth" && path[5] == "generate_private_key" {
			return p
		}
	}
	if pathLen == 3 {
		if path[0] == "" && path[1] == "auth" && path[2] == "generate_private_key" {
			return p
		}
	}
	return nil
}
Пример #15
0
func (p *SetPasswordRequestHandler) HandlerFor(req wm.Request, writer wm.ResponseWriter) wm.RequestHandler {
	// /api/v1/json/auth/set_password
	// /auth/set_password
	path := req.URLParts()
	pathLen := len(path)
	if path[pathLen-1] == "" {
		// ignore trailing slash
		pathLen = pathLen - 1
	}
	if pathLen == 6 {
		if path[0] == "" && path[1] == "api" && path[2] == "v1" && path[3] == "json" && path[4] == "auth" && path[5] == "set_password" {
			return p
		}
	}
	if pathLen == 3 {
		if path[0] == "" && path[1] == "auth" && path[2] == "set_password" {
			return p
		}
	}
	return nil
}
Пример #16
0
func (p *LogoutAccountRequestHandler) HandlerFor(req wm.Request, writer wm.ResponseWriter) wm.RequestHandler {
	// /api/v1/json/auth/logout
	// /auth/logout
	path := req.URLParts()
	pathLen := len(path)
	if path[pathLen-1] == "" {
		// ignore trailing slash
		pathLen = pathLen - 1
	}
	if pathLen == 6 {
		if path[0] == "" && path[1] == "api" && path[2] == "v1" && path[3] == "json" && path[4] == "auth" && path[5] == "logout" {
			return p
		}
	}
	if pathLen == 3 {
		if path[0] == "" && path[1] == "auth" && path[2] == "logout" {
			return p
		}
	}
	return nil
}
Пример #17
0
func (p *ViewContactRequestHandler) HandlerFor(req wm.Request, writer wm.ResponseWriter) wm.RequestHandler {
	// /api/v1/json/u/<uid>/contacts/list
	// /u/<uid>/contacts/list
	path := req.URLParts()
	pathLen := len(path)
	if path[pathLen-1] == "" {
		// ignore trailing slash
		pathLen = pathLen - 1
	}
	if pathLen == 9 {
		if path[0] == "" && path[1] == "api" && path[2] == "v1" && path[3] == "json" && path[4] == "u" && path[6] == "contacts" && path[7] == "view" {
			return p
		}
	}
	if pathLen == 6 {
		if path[0] == "" && path[1] == "u" && path[3] == "contacts" && path[4] == "view" {
			return p
		}
	}
	return nil
}
Пример #18
0
func (p *CreateAccountRequestHandler) Forbidden(req wm.Request, cxt wm.Context) (bool, wm.Request, wm.Context, int, os.Error) {
	cac := cxt.(CreateAccountContext)
	hasSignature, userId, consumerId, err := apiutil.CheckSignature(p.authDS, req.UnderlyingRequest())
	if err != nil {
		return true, req, cxt, 403, err
	}
	if hasSignature {
		if userId != "" {
			user, _ := p.ds.RetrieveUserAccountById(userId)
			cac.SetRequestingUser(user)
		}
		if consumerId != "" {
			consumer, _ := p.ds.RetrieveConsumerAccountById(consumerId)
			cac.SetRequestingConsumer(consumer)
		}
		if (userId != "" && (cac.RequestingUser() == nil || !cac.RequestingUser().Accessible())) && (consumerId != "" && (cac.RequestingConsumer() == nil || !cac.RequestingConsumer().Accessible())) {
			// Cannot find user or consumer with specified id
			return true, req, cxt, 0, nil
		}
	}
	return false, req, cxt, 0, nil
}
func (p *UrlEncodedMediaTypeInputHandler) MediaTypeHandleInputFrom(req wm.Request, cxt wm.Context) (int, http.Header, io.WriterTo) {
	m := req.Form()
	if m == nil || len(m) == 0 {
		if err := req.ParseForm(); err != nil {
			return OutputErrorMessage(err.Error(), nil, http.StatusBadRequest, nil)
		}
		m = req.Form()
	}
	return p.handler.HandleUrlEncodedInputHandler(req, cxt, m)
}
Пример #20
0
func (p *UpdateContactRequestHandler) ContentTypesAccepted(req wm.Request, cxt wm.Context) ([]wm.MediaTypeInputHandler, wm.Request, wm.Context, int, error) {
	arr := []wm.MediaTypeInputHandler{apiutil.NewJSONMediaTypeInputHandler("", "", p, req.Body())}
	return arr, req, cxt, 0, nil
}