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


Golang request.Authenticate函數代碼示例

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


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

示例1: Read

// GET: /client/{id}
func (cr *ClientController) Read(id string, cx *goweb.Context) {
	// Gather query params
	query := &Query{Li: cx.Request.URL.Query()}

	if query.Has("heartbeat") { //handle heartbeat
		cg, err := request.AuthenticateClientGroup(cx.Request)
		if err != nil {
			if err.Error() == e.NoAuth || err.Error() == e.UnAuth || err.Error() == e.InvalidAuth {
				if conf.CLIENT_AUTH_REQ == true {
					cx.RespondWithError(http.StatusUnauthorized)
					return
				}
			} else {
				logger.Error("[email protected]: " + err.Error())
				cx.RespondWithError(http.StatusInternalServerError)
				return
			}
		}

		hbmsg, err := core.QMgr.ClientHeartBeat(id, cg)
		if err != nil {
			cx.RespondWithErrorMessage(err.Error(), http.StatusBadRequest)
		} else {
			cx.RespondWithData(hbmsg)
		}
		return
	}

	LogRequest(cx.Request) //skip heartbeat in access log

	// Try to authenticate user.
	u, err := request.Authenticate(cx.Request)
	if err != nil && err.Error() != e.NoAuth {
		cx.RespondWithErrorMessage(err.Error(), http.StatusUnauthorized)
		return
	}

	// If no auth was provided, and anonymous read is allowed, use the public user
	if u == nil {
		if conf.ANON_READ == true {
			u = &user.User{Uuid: "public"}
		} else {
			cx.RespondWithErrorMessage(e.NoAuth, http.StatusUnauthorized)
			return
		}
	}

	client, err := core.QMgr.GetClientByUser(id, u)
	if err != nil {
		if err.Error() == e.ClientNotFound {
			cx.RespondWithErrorMessage(e.ClientNotFound, http.StatusBadRequest)
		} else {
			logger.Error("Error in GET client:" + err.Error())
			cx.RespondWithError(http.StatusBadRequest)
		}
		return
	}
	cx.RespondWithData(client)
	return
}
開發者ID:MG-RAST,項目名稱:AWE,代碼行數:61,代碼來源:clientController.go

示例2: UpdateMany

// PUT: /logger
func (cr *LoggerController) UpdateMany(cx *goweb.Context) {
	LogRequest(cx.Request)

	// Try to authenticate user.
	u, err := request.Authenticate(cx.Request)
	if err != nil && err.Error() != e.NoAuth {
		cx.RespondWithErrorMessage(err.Error(), http.StatusUnauthorized)
		return
	}
	// must be admin user
	if u == nil || u.Admin == false {
		cx.RespondWithErrorMessage(e.NoAuth, http.StatusUnauthorized)
		return
	}

	// Gather query params
	query := &Query{Li: cx.Request.URL.Query()}

	// currently can only reset debug level
	if query.Has("debug") {
		levelStr := query.Value("debug")
		levelInt, err := strconv.Atoi(levelStr)
		if err != nil {
			cx.RespondWithErrorMessage("invalid debug level: "+err.Error(), http.StatusBadRequest)
		}
		conf.DEBUG_LEVEL = levelInt
		logger.Event(event.DEBUG_LEVEL, "level="+levelStr+";user="+u.Username)
		cx.RespondWithData(map[string]int{"debuglevel": conf.DEBUG_LEVEL})
		return
	}

	cx.RespondWithError(http.StatusNotImplemented)
	return
}
開發者ID:MG-RAST,項目名稱:AWE,代碼行數:35,代碼來源:loggerController.go

示例3: CreateWithId

// POST: /clientgroup/{name}
func (cr *ClientGroupController) CreateWithId(name string, cx *goweb.Context) {
	LogRequest(cx.Request)

	// Try to authenticate user.
	u, err := request.Authenticate(cx.Request)
	if err != nil && err.Error() != e.NoAuth {
		cx.RespondWithErrorMessage(err.Error(), http.StatusUnauthorized)
		return
	}

	// If no auth was provided and ANON_CG_WRITE is true, use the public user.
	// Otherwise if no auth was provided or user is not an admin, and ANON_CG_WRITE is false, throw an error.
	// Otherwise, proceed with creation of the clientgroup with the user.
	if u == nil && conf.ANON_CG_WRITE == true {
		u = &user.User{Uuid: "public"}
	} else if u == nil || !u.Admin {
		if conf.ANON_CG_WRITE == false {
			cx.RespondWithErrorMessage(e.UnAuth, http.StatusUnauthorized)
			return
		}
	}

	cg, err := core.CreateClientGroup(name, u)
	if err != nil {
		cx.RespondWithErrorMessage(err.Error(), http.StatusBadRequest)
		return
	}

	cx.RespondWithData(cg)
	return
}
開發者ID:wtangiit,項目名稱:AWE,代碼行數:32,代碼來源:clientGroupController.go

示例4: UpdateMany

// PUT: /queue
func (cr *QueueController) UpdateMany(cx *goweb.Context) {
	LogRequest(cx.Request)

	// Try to authenticate user.
	u, err := request.Authenticate(cx.Request)
	if err != nil && err.Error() != e.NoAuth {
		cx.RespondWithErrorMessage(err.Error(), http.StatusUnauthorized)
		return
	}
	// must be admin user
	if u == nil || u.Admin == false {
		cx.RespondWithErrorMessage(e.NoAuth, http.StatusUnauthorized)
		return
	}

	// Gather query params
	query := &Query{Li: cx.Request.URL.Query()}

	if query.Has("resume") {
		core.QMgr.ResumeQueue()
		logger.Event(event.QUEUE_RESUME, "user="+u.Username)
		cx.RespondWithData("work queue resumed")
		return
	}
	if query.Has("suspend") {
		core.QMgr.SuspendQueue()
		logger.Event(event.QUEUE_SUSPEND, "user="+u.Username)
		cx.RespondWithData("work queue suspended")
		return
	}

	cx.RespondWithErrorMessage("requested queue operation not supported", http.StatusBadRequest)
	return
}
開發者ID:MG-RAST,項目名稱:AWE,代碼行數:35,代碼來源:queueController.go

示例5: Delete

// DELETE: /job/{id}
func (cr *JobController) Delete(id string, cx *goweb.Context) {
	LogRequest(cx.Request)

	// Try to authenticate user.
	u, err := request.Authenticate(cx.Request)
	if err != nil && err.Error() != e.NoAuth {
		cx.RespondWithErrorMessage(err.Error(), http.StatusUnauthorized)
	}

	// If no auth was provided, and anonymous delete is allowed, use the public user
	if u == nil {
		if conf.ANON_DELETE == true {
			u = &user.User{Uuid: "public"}
		} else {
			cx.RespondWithErrorMessage(e.NoAuth, http.StatusUnauthorized)
			return
		}
	}

	if err = core.QMgr.DeleteJobByUser(id, u); err != nil {
		if err == mgo.ErrNotFound {
			cx.RespondWithNotFound()
			return
		} else if err.Error() == e.UnAuth {
			cx.RespondWithErrorMessage(e.UnAuth, http.StatusUnauthorized)
			return
		} else {
			cx.RespondWithErrorMessage("fail to delete job: "+id, http.StatusBadRequest)
			return
		}
	}

	cx.RespondWithData("job deleted: " + id)
	return
}
開發者ID:edwardt,項目名稱:AWE,代碼行數:36,代碼來源:jobController.go

示例6: UpdateMany

// PUT: /client
func (cr *ClientController) UpdateMany(cx *goweb.Context) {
	LogRequest(cx.Request)

	// Try to authenticate user.
	u, err := request.Authenticate(cx.Request)
	if err != nil && err.Error() != e.NoAuth {
		cx.RespondWithErrorMessage(err.Error(), http.StatusUnauthorized)
		return
	}

	// If no auth was provided, and anonymous read is allowed, use the public user
	if u == nil {
		if conf.ANON_WRITE == true {
			u = &user.User{Uuid: "public"}
		} else {
			cx.RespondWithErrorMessage(e.NoAuth, http.StatusUnauthorized)
			return
		}
	}

	// Gather query params
	query := &Query{Li: cx.Request.URL.Query()}
	if query.Has("resumeall") { //resume the suspended client
		num := core.QMgr.ResumeSuspendedClientsByUser(u)
		cx.RespondWithData(fmt.Sprintf("%d suspended clients resumed", num))
		return
	}
	if query.Has("suspendall") { //resume the suspended client
		num := core.QMgr.SuspendAllClientsByUser(u)
		cx.RespondWithData(fmt.Sprintf("%d clients suspended", num))
		return
	}
	cx.RespondWithError(http.StatusNotImplemented)
	return
}
開發者ID:MG-RAST,項目名稱:AWE,代碼行數:36,代碼來源:clientController.go

示例7: ReadMany

// GET: /user
func (cr *UserController) ReadMany(cx *goweb.Context) {
	// Log Request and check for Auth
	LogRequest(cx.Request)
	u, err := request.Authenticate(cx.Request)

	if err != nil {
		if err.Error() == e.NoAuth || err.Error() == e.UnAuth {
			cx.RespondWithError(http.StatusUnauthorized)
			return
		} else {
			logger.Error("[email protected]_Read: " + err.Error())
			cx.RespondWithError(http.StatusInternalServerError)
			return
		}
	}
	if u.Admin {
		users := user.Users{}
		user.AdminGet(&users)
		if len(users) > 0 {
			cx.RespondWithData(users)
			return
		} else {
			cx.RespondWithNotFound()
			return
		}
	} else {
		cx.RespondWithError(http.StatusUnauthorized)
		return
	}
}
開發者ID:narayandesai,項目名稱:AWE,代碼行數:31,代碼來源:userController.go

示例8: Delete

// DELETE: /client/{id}
func (cr *ClientController) Delete(id string, cx *goweb.Context) {
	LogRequest(cx.Request)

	// Try to authenticate user.
	u, err := request.Authenticate(cx.Request)
	if err != nil && err.Error() != e.NoAuth {
		cx.RespondWithErrorMessage(err.Error(), http.StatusUnauthorized)
		return
	}

	// If no auth was provided, and anonymous read is allowed, use the public user
	if u == nil {
		if conf.ANON_DELETE == true {
			u = &user.User{Uuid: "public"}
		} else {
			cx.RespondWithErrorMessage(e.NoAuth, http.StatusUnauthorized)
			return
		}
	}

	if err := core.QMgr.DeleteClientByUser(id, u); err != nil {
		cx.RespondWithErrorMessage(err.Error(), http.StatusBadRequest)
	} else {
		cx.RespondWithData("client deleted")
	}
	return
}
開發者ID:MG-RAST,項目名稱:AWE,代碼行數:28,代碼來源:clientController.go

示例9: ReadMany

// GET: /client
func (cr *ClientController) ReadMany(cx *goweb.Context) {
	LogRequest(cx.Request)

	// Try to authenticate user.
	u, err := request.Authenticate(cx.Request)
	if err != nil && err.Error() != e.NoAuth {
		cx.RespondWithErrorMessage(err.Error(), http.StatusUnauthorized)
		return
	}

	// If no auth was provided, and anonymous read is allowed, use the public user
	if u == nil {
		if conf.ANON_READ == true {
			u = &user.User{Uuid: "public"}
		} else {
			cx.RespondWithErrorMessage(e.NoAuth, http.StatusUnauthorized)
			return
		}
	}

	clients := core.QMgr.GetAllClientsByUser(u)

	query := &Query{Li: cx.Request.URL.Query()}
	filtered := []*core.Client{}
	if query.Has("busy") {
		for _, client := range clients {
			if client.Current_work_length() > 0 {
				filtered = append(filtered, client)
			}
		}
	} else if query.Has("group") {
		for _, client := range clients {
			if client.Group == query.Value("group") {
				filtered = append(filtered, client)
			}
		}
	} else if query.Has("status") {
		for _, client := range clients {
			status := client.Get_Status()
			stat := strings.Split(status, "-")
			if status == query.Value("status") {
				filtered = append(filtered, client)
			} else if (len(stat) == 2) && (stat[1] == query.Value("status")) {
				filtered = append(filtered, client)
			}
		}
	} else if query.Has("app") {
		for _, client := range clients {
			for _, app := range client.Apps {
				if app == query.Value("app") {
					filtered = append(filtered, client)
				}
			}
		}
	} else {
		filtered = clients
	}
	cx.RespondWithData(filtered)
	return
}
開發者ID:MG-RAST,項目名稱:AWE,代碼行數:61,代碼來源:clientController.go

示例10: DeleteMany

// DELETE: /job?suspend, /job?zombie
func (cr *JobController) DeleteMany(cx *goweb.Context) {
	LogRequest(cx.Request)

	// Try to authenticate user.
	u, err := request.Authenticate(cx.Request)
	if err != nil && err.Error() != e.NoAuth {
		cx.RespondWithErrorMessage(err.Error(), http.StatusUnauthorized)
		return
	}

	// If no auth was provided, and anonymous delete is allowed, use the public user
	if u == nil {
		if conf.ANON_DELETE == true {
			u = &user.User{Uuid: "public"}
		} else {
			cx.RespondWithErrorMessage(e.NoAuth, http.StatusUnauthorized)
			return
		}
	}

	// Gather query params
	query := &Query{Li: cx.Request.URL.Query()}
	if query.Has("suspend") {
		num := core.QMgr.DeleteSuspendedJobsByUser(u)
		cx.RespondWithData(fmt.Sprintf("deleted %d suspended jobs", num))
	} else if query.Has("zombie") {
		num := core.QMgr.DeleteZombieJobsByUser(u)
		cx.RespondWithData(fmt.Sprintf("deleted %d zombie jobs", num))
	} else {
		cx.RespondWithError(http.StatusNotImplemented)
	}
	return
}
開發者ID:edwardt,項目名稱:AWE,代碼行數:34,代碼來源:jobController.go

示例11: Delete

// DELETE: /clientgroup/{id}
func (cr *ClientGroupController) Delete(id string, cx *goweb.Context) {
	LogRequest(cx.Request)

	// Try to authenticate user.
	u, err := request.Authenticate(cx.Request)
	if err != nil && err.Error() != e.NoAuth {
		cx.RespondWithErrorMessage(err.Error(), http.StatusUnauthorized)
		return
	}

	// If no auth was provided and ANON_CG_DELETE is true, use the public user.
	// Otherwise if no auth was provided, throw an error.
	// Otherwise, proceed with deletion of the clientgroup using the user.
	if u == nil {
		if conf.ANON_CG_DELETE == true {
			u = &user.User{Uuid: "public"}
		} else {
			cx.RespondWithErrorMessage(e.UnAuth, http.StatusUnauthorized)
			return
		}
	}

	// Load clientgroup by id
	cg, err := core.LoadClientGroup(id)

	if err != nil {
		if err == mgo.ErrNotFound {
			cx.RespondWithNotFound()
		} else {
			// In theory the db connection could be lost between
			// checking user and load but seems unlikely.
			cx.RespondWithErrorMessage("clientgroup id not found:"+id, http.StatusBadRequest)
		}
		return
	}

	// User must have delete permissions on clientgroup or be clientgroup owner or be an admin or the clientgroup is publicly deletable.
	// The other possibility is that public deletion of clientgroups is enabled and the clientgroup is publicly deletable.
	rights := cg.Acl.Check(u.Uuid)
	public_rights := cg.Acl.Check("public")
	if (u.Uuid != "public" && (cg.Acl.Owner == u.Uuid || rights["delete"] == true || u.Admin == true || public_rights["delete"] == true)) ||
		(u.Uuid == "public" && conf.ANON_CG_DELETE == true && public_rights["delete"] == true) {
		err := core.DeleteClientGroup(id)
		if err != nil {
			cx.RespondWithErrorMessage("Could not delete clientgroup.", http.StatusInternalServerError)
			return
		}
		cx.RespondWithOK()
		return
	}

	cx.RespondWithErrorMessage(e.UnAuth, http.StatusUnauthorized)
	return
}
開發者ID:wtangiit,項目名稱:AWE,代碼行數:55,代碼來源:clientGroupController.go

示例12: Update

// PUT: /client/{id} -> status update
func (cr *ClientController) Update(id string, cx *goweb.Context) {
	LogRequest(cx.Request)

	// Try to authenticate user.
	u, err := request.Authenticate(cx.Request)
	if err != nil && err.Error() != e.NoAuth {
		cx.RespondWithErrorMessage(err.Error(), http.StatusUnauthorized)
		return
	}

	// If no auth was provided, and anonymous read is allowed, use the public user
	if u == nil {
		if conf.ANON_WRITE == true {
			u = &user.User{Uuid: "public"}
		} else {
			cx.RespondWithErrorMessage(e.NoAuth, http.StatusUnauthorized)
			return
		}
	}

	// Gather query params
	query := &Query{Li: cx.Request.URL.Query()}
	if query.Has("subclients") { //update the number of subclients for a proxy
		if count, err := strconv.Atoi(query.Value("subclients")); err != nil {
			cx.RespondWithError(http.StatusNotImplemented)
		} else {
			core.QMgr.UpdateSubClientsByUser(id, count, u)
			cx.RespondWithData("ok")
		}
		return
	}
	if query.Has("suspend") { //resume the suspended client
		if err := core.QMgr.SuspendClientByUser(id, u); err != nil {
			cx.RespondWithErrorMessage(err.Error(), http.StatusBadRequest)
		} else {
			cx.RespondWithData("client suspended")
		}
		return
	}
	if query.Has("resume") { //resume the suspended client
		if err := core.QMgr.ResumeClientByUser(id, u); err != nil {
			cx.RespondWithErrorMessage(err.Error(), http.StatusBadRequest)
		} else {
			cx.RespondWithData("client resumed")
		}
		return
	}
	cx.RespondWithError(http.StatusNotImplemented)
	return
}
開發者ID:MG-RAST,項目名稱:AWE,代碼行數:51,代碼來源:clientController.go

示例13: AdminAuthenticated

func AdminAuthenticated(cx *goweb.Context) bool {
	user, err := request.Authenticate(cx.Request)
	if err != nil {
		if err.Error() == e.NoAuth || err.Error() == e.UnAuth {
			cx.RespondWithError(http.StatusUnauthorized)
		} else {
			request.AuthError(err, cx)
		}
		return false
	}
	if _, ok := conf.Admin_Users[user.Username]; !ok {
		msg := fmt.Sprintf("user %s has no admin right", user.Username)
		cx.RespondWithErrorMessage(msg, http.StatusBadRequest)
		return false
	}
	return true
}
開發者ID:wtangiit,項目名稱:AWE,代碼行數:17,代碼來源:util.go

示例14: Create

// POST: /client
func (cr *ClientController) Create(cx *goweb.Context) {
	// Log Request and check for Auth
	LogRequest(cx.Request)

	_, err := request.Authenticate(cx.Request)
	if err != nil {
		if err.Error() == e.NoAuth || err.Error() == e.UnAuth {
			cx.RespondWithError(http.StatusUnauthorized)
			return
		} else {
			logger.Error("[email protected]_Read: " + err.Error())
			cx.RespondWithError(http.StatusInternalServerError)
			return
		}
	}

	// Parse uploaded form
	_, files, err := ParseMultipartForm(cx.Request)
	if err != nil {
		if err.Error() != "request Content-Type isn't multipart/form-data" {
			logger.Error("Error parsing form: " + err.Error())
			cx.RespondWithError(http.StatusBadRequest)
			return
		}
	}

	client, err := core.QMgr.RegisterNewClient(files)
	if err != nil {
		msg := "Error in registering new client:" + err.Error()
		logger.Error(msg)
		cx.RespondWithErrorMessage(msg, http.StatusBadRequest)
		return
	}

	//log event about client registration (CR)
	logger.Event(event.CLIENT_REGISTRATION, "clientid="+client.Id+";name="+client.Name+";host="+client.Host)

	cx.RespondWithData(client)
	return
}
開發者ID:narayandesai,項目名稱:AWE,代碼行數:41,代碼來源:clientController.go

示例15: Read

// GET: /user/{id}
func (cr *UserController) Read(id string, cx *goweb.Context) {
	// Log Request and check for Auth
	LogRequest(cx.Request)
	u, err := request.Authenticate(cx.Request)
	if err != nil {
		if err.Error() == e.NoAuth || err.Error() == e.UnAuth {
			cx.RespondWithError(http.StatusUnauthorized)
			return
		} else {
			logger.Error("[email protected]_Read: " + err.Error())
			cx.RespondWithError(http.StatusInternalServerError)
			return
		}
	}
	// Any user can access their own user info. Only admins can
	// access other's info
	if u.Uuid == id {
		cx.RespondWithData(u)
		return
	} else if u.Admin {
		nu, err := user.FindByUuid(id)
		if err != nil {
			if err.Error() == e.MongoDocNotFound {
				cx.RespondWithNotFound()
				return
			} else {
				logger.Error("[email protected]_Read:Admin: " + err.Error())
				cx.RespondWithError(http.StatusInternalServerError)
				return
			}
		}
		cx.RespondWithData(nu)
		return
	} else {
		// Not sure how we could end up here but its probably the
		// user's fault
		cx.RespondWithError(http.StatusBadRequest)
		return
	}
}
開發者ID:narayandesai,項目名稱:AWE,代碼行數:41,代碼來源:userController.go


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