當前位置: 首頁>>代碼示例>>Golang>>正文


Golang auth.GetTokenFromContext函數代碼示例

本文整理匯總了Golang中github.com/sensu/uchiwa/uchiwa/auth.GetTokenFromContext函數的典型用法代碼示例。如果您正苦於以下問題:Golang GetTokenFromContext函數的具體用法?Golang GetTokenFromContext怎麽用?Golang GetTokenFromContext使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了GetTokenFromContext函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: aggregatesHandler

// Aggregates
func (u *Uchiwa) aggregatesHandler(w http.ResponseWriter, r *http.Request) {
	if r.Method != "GET" {
		http.Error(w, "", http.StatusBadRequest)
		return
	}

	// verify that the authenticated user is authorized to access this resource
	token := auth.GetTokenFromContext(r)

	resources := strings.Split(r.URL.Path, "/")

	if len(resources) == 2 {
		token := auth.GetTokenFromContext(r)
		aggregates := FilterAggregates(&u.Data.Aggregates, token)

		encoder := json.NewEncoder(w)
		if err := encoder.Encode(aggregates); err != nil {
			http.Error(w, fmt.Sprintf("Cannot encode response data: %v", err), http.StatusInternalServerError)
			return
		}
	} else if len(resources) == 5 {
		check := resources[3]
		dc := resources[2]
		issued := resources[4]

		if check == "" || dc == "" || issued == "" {
			http.Error(w, "", http.StatusBadRequest)
			return
		}

		unauthorized := FilterGetRequest(dc, token)
		if unauthorized {
			http.Error(w, fmt.Sprint(""), http.StatusNotFound)
			return
		}

		aggregate, err := u.GetAggregateByIssued(check, issued, dc)
		if err != nil {
			http.Error(w, fmt.Sprint(err), 500)
			return
		}

		encoder := json.NewEncoder(w)
		if err := encoder.Encode(aggregate); err != nil {
			http.Error(w, fmt.Sprintf("Cannot encode response data: %v", err), http.StatusInternalServerError)
			return
		}
	} else {
		http.Error(w, "", http.StatusBadRequest)
		return
	}
}
開發者ID:postfix,項目名稱:uchiwa,代碼行數:53,代碼來源:server.go

示例2: getAggregateByIssuedHandler

func (u *Uchiwa) getAggregateByIssuedHandler(w http.ResponseWriter, r *http.Request) {
	urlStruct, _ := url.Parse(r.URL.String())
	check := urlStruct.Query().Get("check")
	issued := urlStruct.Query().Get("issued")
	dc := urlStruct.Query().Get("dc")
	if check == "" || issued == "" || dc == "" {
		http.Error(w, fmt.Sprint("Parameters 'check', 'issued' and 'dc' are required"), http.StatusNotFound)
		return
	}

	// verify that the authenticated user is authorized to access this resource
	token := auth.GetTokenFromContext(r)
	unauthorized := FilterGetRequest(dc, token)

	if unauthorized {
		http.Error(w, fmt.Sprint(""), http.StatusNotFound)
		return
	}

	aggregate, err := u.GetAggregateByIssued(check, issued, dc)
	if err != nil {
		http.Error(w, fmt.Sprint(err), 500)
	} else {
		encoder := json.NewEncoder(w)
		if err := encoder.Encode(aggregate); err != nil {
			http.Error(w, fmt.Sprintf("Cannot encode response data: %v", err), http.StatusInternalServerError)
		}
	}
}
開發者ID:traviswdev,項目名稱:uchiwa,代碼行數:29,代碼來源:server.go

示例3: getClientHandler

func (u *Uchiwa) getClientHandler(w http.ResponseWriter, r *http.Request) {
	urlStruct, _ := url.Parse(r.URL.String())
	id := urlStruct.Query().Get("id")
	dc := urlStruct.Query().Get("dc")
	if id == "" || dc == "" {
		http.Error(w, fmt.Sprint("Parameters 'id' and 'dc' are required"), http.StatusNotFound)
		return
	}

	// verify that the authenticated user is authorized to access this resource
	token := auth.GetTokenFromContext(r)
	unauthorized := FilterGetRequest(dc, token)

	if unauthorized {
		http.Error(w, fmt.Sprint(""), http.StatusNotFound)
		return
	}

	client, err := u.GetClient(id, dc)
	if err != nil {
		http.Error(w, fmt.Sprint(err), http.StatusNotFound)
		return
	}

	encoder := json.NewEncoder(w)
	if err := encoder.Encode(client); err != nil {
		http.Error(w, fmt.Sprintf("Cannot encode response data: %v", err), http.StatusInternalServerError)
		return
	}
}
開發者ID:jsoriano,項目名稱:uchiwa,代碼行數:30,代碼來源:server.go

示例4: resultsHandler

// Results
func (u *Uchiwa) resultsHandler(w http.ResponseWriter, r *http.Request) {
	if r.Method != "DELETE" {
		http.Error(w, "", http.StatusBadRequest)
		return
	}

	resources := strings.Split(r.URL.Path, "/")
	if len(resources) != 5 {
		http.Error(w, "", http.StatusBadRequest)
		return
	}

	check := resources[4]
	client := resources[3]
	dc := resources[2]

	token := auth.GetTokenFromContext(r)

	unauthorized := FilterGetRequest(dc, token)
	if unauthorized {
		http.Error(w, fmt.Sprint(""), http.StatusNotFound)
		return
	}

	err := u.DeleteCheckResult(check, client, dc)
	if err != nil {
		http.Error(w, fmt.Sprint(err), http.StatusInternalServerError)
		return
	}
}
開發者ID:matthelgen,項目名稱:uchiwa,代碼行數:31,代碼來源:server.go

示例5: stashHandler

func (u *Uchiwa) stashHandler(w http.ResponseWriter, r *http.Request) {
	if r.Method != "POST" {
		http.Error(w, "", http.StatusBadRequest)
		return
	}

	decoder := json.NewDecoder(r.Body)
	var data stash
	err := decoder.Decode(&data)
	if err != nil {
		http.Error(w, "Could not decode body", http.StatusInternalServerError)
		return
	}

	// verify that the authenticated user is authorized to access this resource
	token := auth.GetTokenFromContext(r)
	unauthorized := FilterGetRequest(data.Dc, token)

	// add username to the stash content
	if token != nil && token.Claims["Username"] != nil {
		data.Content["username"] = token.Claims["Username"]
	}

	if unauthorized {
		http.Error(w, fmt.Sprint(""), http.StatusNotFound)
		return
	}

	err = u.PostStash(data)
	if err != nil {
		http.Error(w, "Could not create the stash", http.StatusNotFound)
	}
}
開發者ID:traviswdev,項目名稱:uchiwa,代碼行數:33,代碼來源:server.go

示例6: requestHandler

// Request
func (u *Uchiwa) requestHandler(w http.ResponseWriter, r *http.Request) {
	if r.Method != "POST" {
		http.Error(w, "", http.StatusBadRequest)
		return
	}

	decoder := json.NewDecoder(r.Body)
	var data structs.CheckExecution
	err := decoder.Decode(&data)
	if err != nil {
		http.Error(w, "Could not decode body", http.StatusInternalServerError)
		return
	}

	// verify that the authenticated user is authorized to access this resource
	token := auth.GetTokenFromContext(r)
	unauthorized := FilterGetRequest(data.Dc, token)
	if unauthorized {
		http.Error(w, fmt.Sprint(""), http.StatusNotFound)
		return
	}

	err = u.IssueCheckExecution(data)
	if err != nil {
		http.Error(w, "Could not create the stash", http.StatusNotFound)
		return
	}

	return
}
開發者ID:matthelgen,項目名稱:uchiwa,代碼行數:31,代碼來源:server.go

示例7: stashesHandler

// Stashes
func (u *Uchiwa) stashesHandler(w http.ResponseWriter, r *http.Request) {
	token := auth.GetTokenFromContext(r)
	resources := strings.Split(r.URL.Path, "/")

	if r.Method == "DELETE" && len(resources) >= 3 {
		dc := resources[2]
		path := strings.Join(resources[3:], "/")

		unauthorized := FilterGetRequest(dc, token)
		if unauthorized {
			http.Error(w, fmt.Sprint(""), http.StatusNotFound)
			return
		}

		err := u.DeleteStash(dc, path)
		if err != nil {
			http.Error(w, "Could not create the stash", http.StatusNotFound)
		}
	} else if r.Method == "GET" {
		stashes := FilterStashes(&u.Data.Stashes, token)

		encoder := json.NewEncoder(w)
		if err := encoder.Encode(stashes); err != nil {
			http.Error(w, fmt.Sprintf("Cannot encode response data: %v", err), http.StatusInternalServerError)
			return
		}
	} else if r.Method == "POST" {
		decoder := json.NewDecoder(r.Body)
		var data stash
		err := decoder.Decode(&data)
		if err != nil {
			http.Error(w, "Could not decode body", http.StatusInternalServerError)
			return
		}

		// verify that the authenticated user is authorized to access this resource
		unauthorized := FilterGetRequest(data.Dc, token)
		if unauthorized {
			http.Error(w, fmt.Sprint(""), http.StatusNotFound)
			return
		}

		if token != nil && token.Claims["Username"] != nil {
			data.Content["username"] = token.Claims["Username"]
		}

		err = u.PostStash(data)
		if err != nil {
			http.Error(w, "Could not create the stash", http.StatusNotFound)
			return
		}
	} else {
		http.Error(w, "", http.StatusBadRequest)
		return
	}
}
開發者ID:postfix,項目名稱:uchiwa,代碼行數:57,代碼來源:server.go

示例8: getSensuHandler

func (u *Uchiwa) getSensuHandler(w http.ResponseWriter, r *http.Request) {
	//Test()
	token := auth.GetTokenFromContext(r)
	data := FilterSensuData(token, u.Data)

	encoder := json.NewEncoder(w)
	if err := encoder.Encode(data); err != nil {
		http.Error(w, fmt.Sprintf("Cannot encode response data: %v", err), http.StatusInternalServerError)
	}
}
開發者ID:traviswdev,項目名稱:uchiwa,代碼行數:10,代碼來源:server.go

示例9: stashesHandler

// stashes
func (u *Uchiwa) stashesHandler(w http.ResponseWriter, r *http.Request) {
	if r.Method == "GET" {
		token := auth.GetTokenFromContext(r)
		stashes := FilterStashes(&u.Data.Stashes, token)

		encoder := json.NewEncoder(w)
		if err := encoder.Encode(stashes); err != nil {
			http.Error(w, fmt.Sprintf("Cannot encode response data: %v", err), http.StatusInternalServerError)
			return
		}
	} else if r.Method == "POST" {
		decoder := json.NewDecoder(r.Body)
		var data stash
		err := decoder.Decode(&data)
		if err != nil {
			http.Error(w, "Could not decode body", http.StatusInternalServerError)
			return
		}

		// verify that the authenticated user is authorized to access this resource
		token := auth.GetTokenFromContext(r)
		unauthorized := FilterGetRequest(data.Dc, token)

		if unauthorized {
			http.Error(w, fmt.Sprint(""), http.StatusNotFound)
			return
		}

		err = u.PostStash(data)
		if err != nil {
			http.Error(w, "Could not create the stash", http.StatusNotFound)
			return
		}
	} else {
		http.Error(w, "", http.StatusBadRequest)
		return
	}
}
開發者ID:jsoriano,項目名稱:uchiwa,代碼行數:39,代碼來源:server.go

示例10: subscriptionsHandler

// Subscriptions
func (u *Uchiwa) subscriptionsHandler(w http.ResponseWriter, r *http.Request) {
	if r.Method != "GET" {
		http.Error(w, "", http.StatusBadRequest)
		return
	}

	token := auth.GetTokenFromContext(r)
	subscriptions := FilterSubscriptions(&u.Data.Subscriptions, token)

	encoder := json.NewEncoder(w)
	if err := encoder.Encode(subscriptions); err != nil {
		http.Error(w, fmt.Sprintf("Cannot encode response data: %v", err), http.StatusInternalServerError)
		return
	}
}
開發者ID:matthelgen,項目名稱:uchiwa,代碼行數:16,代碼來源:server.go

示例11: eventsHandler

// Events
func (u *Uchiwa) eventsHandler(w http.ResponseWriter, r *http.Request) {
	token := auth.GetTokenFromContext(r)

	// DELETE on /events
	if r.Method == "DELETE" {
		resources := strings.Split(r.URL.Path, "/")
		if len(resources) != 5 {
			http.Error(w, "", http.StatusBadRequest)
			return
		}

		check := resources[4]
		client := resources[3]
		dc := resources[2]

		unauthorized := FilterGetRequest(dc, token)
		if unauthorized {
			http.Error(w, fmt.Sprint(""), http.StatusNotFound)
			return
		}

		err := u.ResolveEvent(check, client, dc)
		if err != nil {
			http.Error(w, fmt.Sprint(err), http.StatusInternalServerError)
			return
		}

	} else if r.Method == "GET" {
		// GET on /events
		events := FilterEvents(&u.Data.Events, token)

		// Create header
		w.Header().Add("Accept-Charset", "utf-8")
		w.Header().Add("Content-Type", "application/json")
		w.Header().Set("Content-Encoding", "gzip")

		gz := gzip.NewWriter(w)
		defer gz.Close()
		if err := json.NewEncoder(gz).Encode(events); err != nil {
			http.Error(w, fmt.Sprintf("Cannot encode response data: %v", err), http.StatusInternalServerError)
			return
		}
		return
	} else {
		http.Error(w, "", http.StatusBadRequest)
		return
	}
}
開發者ID:matthelgen,項目名稱:uchiwa,代碼行數:49,代碼來源:server.go

示例12: datacentersHandler

// Datacenters
func (u *Uchiwa) datacentersHandler(w http.ResponseWriter, r *http.Request) {
	if r.Method != "GET" {
		http.Error(w, "", http.StatusBadRequest)
		return
	}

	token := auth.GetTokenFromContext(r)
	datacenters := FilterDatacenters(u.Data.Dc, token)

	// Create header
	w.Header().Add("Accept-Charset", "utf-8")
	w.Header().Add("Content-Type", "application/json")
	w.Header().Set("Content-Encoding", "gzip")

	gz := gzip.NewWriter(w)
	defer gz.Close()
	if err := json.NewEncoder(gz).Encode(datacenters); err != nil {
		http.Error(w, fmt.Sprintf("Cannot encode response data: %v", err), http.StatusInternalServerError)
		return
	}
	return
}
開發者ID:matthelgen,項目名稱:uchiwa,代碼行數:23,代碼來源:server.go

示例13: eventsHandler

// Events
func (u *Uchiwa) eventsHandler(w http.ResponseWriter, r *http.Request) {
	token := auth.GetTokenFromContext(r)

	if r.Method == "DELETE" {
		resources := strings.Split(r.URL.Path, "/")
		if len(resources) != 5 {
			http.Error(w, "", http.StatusBadRequest)
			return
		}

		check := resources[4]
		client := resources[3]
		dc := resources[2]

		unauthorized := FilterGetRequest(dc, token)
		if unauthorized {
			http.Error(w, fmt.Sprint(""), http.StatusNotFound)
			return
		}

		err := u.ResolveEvent(check, client, dc)
		if err != nil {
			http.Error(w, fmt.Sprint(err), http.StatusInternalServerError)
			return
		}

	} else if r.Method == "GET" {
		events := FilterEvents(&u.Data.Events, token)

		encoder := json.NewEncoder(w)
		if err := encoder.Encode(events); err != nil {
			http.Error(w, fmt.Sprintf("Cannot encode response data: %v", err), http.StatusInternalServerError)
			return
		}
	} else {
		http.Error(w, "", http.StatusBadRequest)
		return
	}
}
開發者ID:postfix,項目名稱:uchiwa,代碼行數:40,代碼來源:server.go

示例14: postEventHandler

func (u *Uchiwa) postEventHandler(w http.ResponseWriter, r *http.Request) {
	decoder := json.NewDecoder(r.Body)
	var data interface{}
	err := decoder.Decode(&data)
	if err != nil {
		http.Error(w, fmt.Sprint("Could not decode body"), http.StatusInternalServerError)
		return
	}

	// verify that the authenticated user is authorized to access this resource
	token := auth.GetTokenFromContext(r)
	unauthorized := FilterPostRequest(token, &data)

	if unauthorized {
		http.Error(w, fmt.Sprint(""), http.StatusNotFound)
		return
	}

	err = u.ResolveEvent(data)

	if err != nil {
		http.Error(w, fmt.Sprint(err), http.StatusInternalServerError)
	}
}
開發者ID:traviswdev,項目名稱:uchiwa,代碼行數:24,代碼來源:server.go

示例15: aggregatesHandler

// Aggregates
func (u *Uchiwa) aggregatesHandler(w http.ResponseWriter, r *http.Request) {
	if r.Method != "GET" {
		http.Error(w, "", http.StatusBadRequest)
		return
	}

	// verify that the authenticated user is authorized to access this resource
	token := auth.GetTokenFromContext(r)

	resources := strings.Split(r.URL.Path, "/")

	// GET on /aggregates
	if len(resources) == 2 {
		token := auth.GetTokenFromContext(r)
		aggregates := FilterAggregates(&u.Data.Aggregates, token)

		// Create header
		w.Header().Add("Accept-Charset", "utf-8")
		w.Header().Add("Content-Type", "application/json")
		w.Header().Set("Content-Encoding", "gzip")

		gz := gzip.NewWriter(w)
		defer gz.Close()
		if err := json.NewEncoder(gz).Encode(aggregates); err != nil {
			http.Error(w, fmt.Sprintf("Cannot encode response data: %v", err), http.StatusInternalServerError)
			return
		}
		return
	} else if len(resources) == 5 {
		// GET on /aggregates/{dc}/{check}/{issued}
		check := resources[3]
		dc := resources[2]
		issued := resources[4]

		if check == "" || dc == "" || issued == "" {
			http.Error(w, "", http.StatusBadRequest)
			return
		}

		unauthorized := FilterGetRequest(dc, token)
		if unauthorized {
			http.Error(w, fmt.Sprint(""), http.StatusNotFound)
			return
		}

		aggregate, err := u.GetAggregateByIssued(check, issued, dc)
		if err != nil {
			http.Error(w, fmt.Sprint(err), 500)
			return
		}

		encoder := json.NewEncoder(w)
		if err := encoder.Encode(aggregate); err != nil {
			http.Error(w, fmt.Sprintf("Cannot encode response data: %v", err), http.StatusInternalServerError)
			return
		}
	} else {
		http.Error(w, "", http.StatusBadRequest)
		return
	}
}
開發者ID:matthelgen,項目名稱:uchiwa,代碼行數:62,代碼來源:server.go


注:本文中的github.com/sensu/uchiwa/uchiwa/auth.GetTokenFromContext函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。