예제 #1
0
//CreateContract will save contract
func CreateContract(w http.ResponseWriter, r *http.Request, includedparty contractdb.Party) {
	contract := &contractdb.Contract{}
	if err := json.NewDecoder(r.Body).Decode(contract); err != nil {
		http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
		return
	}
	if contract.ContractId == "" {
		log.Debug("ContractId can not be empty")
		http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
		return
	}
	hasuser := false
	for _, party := range contract.Parties {
		if party.Type == includedparty.Type && party.Name == includedparty.Name {
			hasuser = true
		}
	}
	if !hasuser {
		contract.Parties = append(contract.Parties, includedparty)
	}
	contractMngr := contractdb.NewManager(r)
	err := contractMngr.Save(contract)
	if err != nil {
		log.Error("ERROR while saving contracts :\n", err)
		http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
		return
	}
	w.WriteHeader(http.StatusCreated)
	json.NewEncoder(w).Encode(&contract)
}
예제 #2
0
// Sign a contract
// It is handler for POST /contracts/{contractId}/signatures
func (api ContractsAPI) contractIdsignaturesPost(w http.ResponseWriter, r *http.Request) {
	contractID := mux.Vars(r)["contractId"]
	contractMngr := contractdb.NewManager(r)
	exists, err := contractMngr.Exists(contractID)
	if err != nil || !exists {
		if err != nil {
			log.Error(err)
		}
		http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
		return
	}
	var signature contractdb.Signature
	if err = json.NewDecoder(r.Body).Decode(&signature); err != nil {
		log.Error(err)
		http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
		return
	}
	err = contractMngr.AddSignature(contractID, signature)
	if err != nil {
		log.Error(err)
		http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
		return
	}
	http.Error(w, http.StatusText(http.StatusCreated), http.StatusCreated)
	return
}
예제 #3
0
// Get a contract
// contractIdGet is handler for GET /contracts/{contractId}
func (api ContractsAPI) contractIdGet(w http.ResponseWriter, r *http.Request) {
	contractID := mux.Vars(r)["contractId"]
	contractMngr := contractdb.NewManager(r)
	contract, err := contractMngr.Get(contractID)
	if err != nil {
		log.Error(err)
		http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
	}

	json.NewEncoder(w).Encode(&contract)
}
// Handler return HTTP handler representation of this middleware
func (om *Oauth2oauth_2_0Middleware) Handler(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

		var atscopestring string
		var username string
		accessToken := om.GetAccessToken(r)
		if accessToken != "" {
			//TODO: cache
			oauthMgr := oauthservice.NewManager(r)
			at, err := oauthMgr.GetAccessToken(accessToken)
			if err != nil {
				log.Error(err)
				http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
				return
			}
			if at == nil {
				http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
				return
			}
			atscopestring = at.Scope
			username = at.Username
		} else {
			w.WriteHeader(401)
			return
		}
		scopes := []string{}

		contractID := mux.Vars(r)["contractId"]
		contractMngr := contractdb.NewManager(r)
		isParticipant, err := contractMngr.IsParticipant(contractID, username)
		if err != nil {
			log.Error(err)
			http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
			return
		}
		if isParticipant {
			scopes = append(scopes, "contract:participant")
			scopes = append(scopes, "contract:read")
		}
		log.Debug("Available scopes: ", scopes)
		log.Debug("Atscopestring scope: ", atscopestring)

		// check scopes
		if !om.CheckScopes(scopes) {
			w.WriteHeader(403)
			return
		}

		next.ServeHTTP(w, r)
	})
}
예제 #5
0
//FindContracts for query
func FindContracts(w http.ResponseWriter, r *http.Request, includedparty contractdb.Party) {
	contractMngr := contractdb.NewManager(r)
	includeexpired := false
	if r.URL.Query().Get("includeExpired") == "true" {
		includeexpired = true
	}
	startstr := r.URL.Query().Get("start")
	start := 0
	var err error
	if startstr != "" {
		start, err = strconv.Atoi(startstr)
		if err != nil {
			log.Error("ERROR while parsing start :\n", err)
			http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
			return
		}
	}
	maxstr := r.URL.Query().Get("max")
	max := 50
	if maxstr != "" {
		max, err = strconv.Atoi(maxstr)
		if err != nil {
			log.Error("ERROR while parsing start :\n", err)
			http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
			return
		}
	}

	contracts, err := contractMngr.GetByIncludedParty(&includedparty, start, max, includeexpired)
	if err != nil {
		log.Error("ERROR while getting contracts :\n", err)
		http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
		return
	}
	w.Header().Set("Content-type", "application/json")
	json.NewEncoder(w).Encode(&contracts)
}