本文整理汇总了Golang中github.com/Masterminds/cookoo.Context.Datasource方法的典型用法代码示例。如果您正苦于以下问题:Golang Context.Datasource方法的具体用法?Golang Context.Datasource怎么用?Golang Context.Datasource使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/Masterminds/cookoo.Context
的用法示例。
在下文中一共展示了Context.Datasource方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Basic
/**
* Perform authentication.
*
* Params:
* - realm (string): The name of the realm. (Default: "web")
* - datasource (string): The name of the datasource that should be used to authenticate.
* This datasource must be an `auth.UserDatasource`. (Default: "auth.UserDatasource")
*
* Context:
* - http.Request (*http.Request): The HTTP request. This is usually placed into the
* context for you.
* - http.ResponseWriter (http.ResponseWriter): The response. This is usually placed
* into the context for you.
*
* Datasource:
* - An auth.UserDatasource. By default, this will look for a datasource named
* "auth.UserDatasource". This can be overridden by the `datasource` param.
*
* Returns:
* - True if the user authenticated. If not, this will send a 401 and then stop
* the current chain.
*/
func Basic(c cookoo.Context, p *cookoo.Params) (interface{}, cookoo.Interrupt) {
realm := p.Get("realm", "web").(string)
dsName := p.Get("datasource", "auth.UserDatasource").(string)
req := c.Get("http.Request", nil).(*http.Request)
res := c.Get("http.ResponseWriter", nil).(http.ResponseWriter)
ds := c.Datasource(dsName).(UserDatasource)
authz := strings.TrimSpace(req.Header.Get("Authorization"))
if len(authz) == 0 || !strings.Contains(authz, "Basic ") {
return sendUnauthorized(realm, res)
}
user, pass, err := parseBasicString(authz)
if err != nil {
c.Logf("info", "Basic authentication parsing failed: %s", err)
return sendUnauthorized(realm, res)
}
ok, err := ds.AuthUser(user, pass)
if !ok {
if err != nil {
c.Logf("info", "Basic authentication caused an error: %s", err)
}
return sendUnauthorized(realm, res)
}
return ok, err
}
示例2: ds
func ds(c cookoo.Context) *Datasource {
return c.Datasource(DSName).(*Datasource)
}