• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Golang oauth2.Tokens类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中github.com/martini-contrib/oauth2.Tokens的典型用法代码示例。如果您正苦于以下问题:Golang Tokens类的具体用法?Golang Tokens怎么用?Golang Tokens使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了Tokens类的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: getUserInfoCached

func getUserInfoCached(tokens oauth2.Tokens) (userObject map[string]interface{}) {

	if val, ok := userObjectCache[tokens.Access()]; ok {
		userObject = val
	}
	return
}
开发者ID:malston,项目名称:pezauth,代码行数:7,代码来源:auth.go


示例2: CurrentUser

// CurrentUser returns current signed in user object
func CurrentUser(tokens oauth2.Tokens) *User {
	profile, _ := GoogleGetProfile(tokens.Access())
	email := profile.Email
	name := profile.Name

	user := User{Email: email}
	db.Where(&user).First(&user)

	if user.ID == 0 {
		db.Unscoped().Where(&user).First(&user)
	}

	if len(user.Name) == 0 && len(name) > 0 {
		user.Name = name
		db.Save(&user)

		setting := &Setting{UserID: int(user.ID)}
		db.Create(setting)
	}

	if user.DeletedAt != nil {
		user.DeletedAt = nil
		db.Unscoped().Save(&user)

		if len(user.Username) > 0 {
			path := filepath.Join(HomeDir(), "app/public/files/"+user.Username)
			os.Mkdir(path, 0755)
		}
	}

	return &user
}
开发者ID:tpiha,项目名称:barecv,代码行数:33,代码来源:auth.go


示例3: DomainChecker

//DomainChecker - check the authenticated users domain to see if it is in the whitelist
func DomainChecker(res http.ResponseWriter, tokens oauth2.Tokens) {
	userInfo := GetUserInfo(tokens)

	if domain, ok := userInfo["domain"]; !ok || tokens.Expired() || isBlockedDomain(domain.(string)) {
		res.WriteHeader(FailureStatus)
		res.Write(AuthFailureResponse)
	}
}
开发者ID:malston,项目名称:pezauth,代码行数:9,代码来源:auth.go


示例4: EnsureSessionVariables

func EnsureSessionVariables(s sessions.Session, t oauth2.Tokens) {
	userName := s.Get("userName")
	twitchId := s.Get("twitchId")
	if userName == nil || twitchId == nil {
		u, err := GetTwitchUser(t.Access())
		if err != nil {
			log.Fatal(err)
		}

		s.Set("userName", u.DisplayName)
		s.Set("twitchId", u.Id)
	}
}
开发者ID:natebeck,项目名称:manfred,代码行数:13,代码来源:web.go


示例5: SocialSignIn

// github && google && ...
func SocialSignIn(tokens oauth2.Tokens) {
	transport := &oauth.Transport{}
	transport.Token = &oauth.Token{
		AccessToken:  tokens.Access(),
		RefreshToken: tokens.Refresh(),
		Expiry:       tokens.ExpiryTime(),
		Extra:        tokens.ExtraData(),
	}

	// Github API refer: https://developer.github.com/v3/users/
	// FIXME: need to judge url
	type GithubUser struct {
		Id    int    `json:"id"`
		Name  string `json:"login"`
		Email string `json:"email"`
	}

	// Make the request.
	scope := "https://api.github.com/user"
	r, err := transport.Client().Get(scope)
	if err != nil {
		log.Error("connect with github error: %s", err)
		// FIXME: handle error page
		return
	}
	defer r.Body.Close()

	user := &GithubUser{}
	err = json.NewDecoder(r.Body).Decode(user)
	if err != nil {
		log.Error("Get: %s", err)
	}
	log.Info("login: %s", user.Name)
	// FIXME: login here, user email to check auth, if not registe, then generate a uniq username
}
开发者ID:pengux,项目名称:gogs,代码行数:36,代码来源:user.go


示例6: Authenticate

func (a *GitHubAuth) Authenticate(organizations []string, c martini.Context, tokens oauth2.Tokens, w http.ResponseWriter, r *http.Request) {
	if len(organizations) > 0 {
		req, err := http.NewRequest("GET", fmt.Sprintf("%s/user/orgs", a.conf.Auth.Info.ApiEndpoint), nil)
		if err != nil {
			log.Printf("failed to create a request to retrieve organizations: %s", err)
			forbidden(w)
			return
		}

		req.SetBasicAuth(tokens.Access(), "x-oauth-basic")

		client := http.Client{}
		res, err := client.Do(req)
		if err != nil {
			log.Printf("failed to retrieve organizations: %s", err)
			forbidden(w)
			return
		}

		data, err := ioutil.ReadAll(res.Body)
		res.Body.Close()

		if err != nil {
			log.Printf("failed to read body of GitHub response: %s", err)
			forbidden(w)
			return
		}

		var info []map[string]interface{}
		if err := json.Unmarshal(data, &info); err != nil {
			log.Printf("failed to decode json: %s", err.Error())
			forbidden(w)
			return
		}

		for _, userOrg := range info {
			for _, org := range organizations {
				if userOrg["login"] == org {
					return
				}
			}
		}

		log.Print("not a member of designated organizations")
		forbidden(w)
		return
	}
}
开发者ID:yasohshiro,项目名称:gate,代码行数:48,代码来源:authenticator.go


示例7: GetLoggedInUser

// Get the logged in user
func GetLoggedInUser(tokens oauth2.Tokens, r render.Render) (int, string) {

	var url = "https://www.googleapis.com/plus/v1/people/me?access_token=" + tokens.Access()

	resp, err := http.Get(url)
	if err != nil {
		panic(err)
	}

	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		panic(err)
	}

	return 200, string(body)
}
开发者ID:rolandjudd,项目名称:thingstodo,代码行数:18,代码来源:user.go


示例8: oauth_get

func oauth_get(url string, tokens oauth2.Tokens) []byte {
	req, err := http.NewRequest("GET", url, nil)
	if err != nil {
		log.Fatal(err)
	}
	req.Header.Set("Authorization", "Bearer "+tokens.Access())

	client := http.Client{}
	res, err := client.Do(req)
	if err != nil {
		log.Fatal(err)
	}

	body, err := ioutil.ReadAll(res.Body)
	res.Body.Close()
	if err != nil {
		log.Fatal(err)
	}
	return body
}
开发者ID:Jonty,项目名称:cf-oauth-example,代码行数:20,代码来源:main.go


示例9: getUserInfo

func getUserInfo(tokens oauth2.Tokens) (userObject map[string]interface{}) {
	url := "https://www.googleapis.com/plus/v1/people/me"
	token := &goauth2.Token{
		AccessToken:  tokens.Access(),
		TokenType:    "Bearer",
		RefreshToken: tokens.Refresh(),
		Expiry:       tokens.ExpiryTime(),
	}
	client := OauthConfig.Client(goauth2.NoContext, token)
	resp, _ := client.Get(url)
	defer resp.Body.Close()
	body, _ := ioutil.ReadAll(resp.Body)
	json.Unmarshal(body, &userObject)
	addUserObjectToCache(tokens, userObject)
	return
}
开发者ID:malston,项目名称:pezauth,代码行数:16,代码来源:auth.go


示例10: RequestData

/* Middleware to:
   - Retrieve data from github if user is logged in and data is not stored in sessions
   - Delete session data if user logged out */
func RequestData(tokens oauth2.Tokens, session sessions.Session) {
	if !tokens.Expired() && session.Get("username") == nil {
		access := tokens.Access()
		req, _ := http.NewRequest("GET", "https://api.github.com/user?access_token="+access, nil)
		client := &http.Client{}
		resp, _ := client.Do(req)

		body, _ := ioutil.ReadAll(resp.Body)

		parse := map[string]interface{}{}
		json.Unmarshal([]byte(string(body)), &parse)
		session.Set("username", parse["login"])
		session.Set("user_id", parse["id"])
		session.Set("avatar", parse["avatar_url"])

		log.Println("retrieved data")
	} else if tokens.Expired() && session.Get("username") != nil {
		session.Clear()
		log.Println("removed data")
	}
}
开发者ID:TimothyGu,项目名称:website,代码行数:24,代码来源:middleware.go


示例11: addUserObjectToCache

func addUserObjectToCache(tokens oauth2.Tokens, userObject map[string]interface{}) (err error) {
	userObjectCache[tokens.Access()] = userObject
	return
}
开发者ID:malston,项目名称:pezauth,代码行数:4,代码来源:auth.go



注:本文中的github.com/martini-contrib/oauth2.Tokens类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang render.Renderer函数代码示例发布时间:2022-05-23
下一篇:
Golang gzip.All函数代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap