分享
  1. 首页
  2. 文章

Go实战--golang中OAuth2.0的使用(使用google账号进行登陆验证)

wangshubo1989 · · 11105 次点击 · · 开始浏览
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

生命不止,继续 go go go!!!

今天继续分享golang中的认证问题,之前写过两篇:

一篇是关于basic认证:Go实战–通过basic认证的http(basic authentication)

一篇是关于JWT的:Go实战–golang中使用JWT(JSON Web Token)

这里就介绍一下golang中使用oauth2.0.

OAuth2.0

OAuth2.0是OAuth协议的下一版本,但不向后兼容OAuth 1.0即完全废止了OAuth1.0。 OAuth 2.0关注客户端开发者的简易性。要么通过组织在资源拥有者和HTTP服务商之间的被批准的交互动作代表用户,要么允许第三方应用代表用户获得访问的权限。同时为Web应用,桌面应用和手机,和起居室设备提供专门的认证流程。2012年10月,OAuth 2.0协议正式发布为RFC 6749.

在认证和授权的过程中涉及的三方包括:
1、服务提供方,用户使用服务提供方来存储受保护的资源,如照片,视频,联系人列表。
2、用户,存放在服务提供方的受保护的资源的拥有者。
3、客户端,要访问服务提供方资源的第三方应用,通常是网站,如提供照片打印服务的网站。在认证过程之前,客户端要向服务提供者申请客户端标识。

使用OAuth进行认证和授权的过程如下所示:

(A)用户打开客户端以后,客户端要求用户给予授权。
(B)用户同意给予客户端授权。
(C)客户端使用上一步获得的授权,向认证服务器申请令牌。
(D)认证服务器对客户端进行认证以后,确认无误,同意发放令牌。
(E)客户端使用令牌,向资源服务器申请获取资源。
(F)资源服务器确认令牌无误,同意向客户端开放资源

这里写图片描述

更详细的内容,可以参考:http://www.ruanyifeng.com/blog/2014/05/oauth_2_0.html

package oauth2

Package oauth2 provides support for making OAuth2 authorized and authenticated HTTP requests. It can additionally grant authorization with Bearer JWT.

获取:

go get golang.org/x/oauth2

type Config

type Config struct {
 // ClientID is the application's ID.
 ClientID string
 // ClientSecret is the application's secret.
 ClientSecret string
 // Endpoint contains the resource server's token endpoint
 // URLs. These are constants specific to each server and are
 // often available via site-specific packages, such as
 // google.Endpoint or github.Endpoint.
 Endpoint Endpoint
 // RedirectURL is the URL to redirect users going through
 // the OAuth flow, after the resource owner's URLs.
 RedirectURL string
 // Scope specifies optional requested permissions.
 Scopes []string
}

func (*Config) AuthCodeURL

func (c *Config) AuthCodeURL(state string, opts ...AuthCodeOption) string

AuthCodeURL returns a URL to OAuth 2.0 provider’s consent page that asks for permissions for the required scopes explicitly.

func (*Config) Exchange

func (c *Config) Exchange(ctx context.Context, code string) (*Token, error)

Exchange converts an authorization code into a token.

type Endpoint

type Endpoint struct {
 AuthURL string
 TokenURL string
}

Endpoint contains the OAuth 2.0 provider’s authorization and token endpoint URLs.

使用google账号进行登陆验证

1.去Google Cloud Platform,创建一个项目

2.凭据,创建凭据,选择OAuth客户端ID
这里写图片描述

3.
选择应用类型为"网页应用"
输入JavaScript来源:http://localhost:8000
输入已获授权的重定向URI:http://localhost:8000/GoogleCallback
这里写图片描述

4.记录下客户端ID和客户端密钥
这里写图片描述

编码

package main
import (
 "fmt"
 "io/ioutil"
 "net/http"
 "golang.org/x/oauth2"
)
const htmlIndex = `<html><body>
<a href="/GoogleLogin">Log in with Google</a>
</body></html>
`
var endpotin = oauth2.Endpoint{
 AuthURL: "https://accounts.google.com/o/oauth2/auth",
 TokenURL: "https://accounts.google.com/o/oauth2/token",
}
var googleOauthConfig = &oauth2.Config{
 ClientID: "your_client_id",
 ClientSecret: "your_client_secret",
 RedirectURL: "http://localhost:8000/GoogleCallback",
 Scopes: []string{"https://www.googleapis.com/auth/userinfo.profile",
 "https://www.googleapis.com/auth/userinfo.email"},
 Endpoint: endpotin,
}
const oauthStateString = "random"
func main() {
 http.HandleFunc("/", handleMain)
 http.HandleFunc("/GoogleLogin", handleGoogleLogin)
 http.HandleFunc("/GoogleCallback", handleGoogleCallback)
 fmt.Println(http.ListenAndServe(":8000", nil))
}
func handleMain(w http.ResponseWriter, r *http.Request) {
 fmt.Fprintf(w, htmlIndex)
}
func handleGoogleLogin(w http.ResponseWriter, r *http.Request) {
 url := googleOauthConfig.AuthCodeURL(oauthStateString)
 fmt.Println(url)
 http.Redirect(w, r, url, http.StatusTemporaryRedirect)
}
func handleGoogleCallback(w http.ResponseWriter, r *http.Request) {
 state := r.FormValue("state")
 if state != oauthStateString {
 fmt.Printf("invalid oauth state, expected '%s', got '%s'\n", oauthStateString, state)
 http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
 return
 }
 fmt.Println(state)
 code := r.FormValue("code")
 fmt.Println(code)
 token, err := googleOauthConfig.Exchange(oauth2.NoContext, code)
 fmt.Println(token)
 if err != nil {
 fmt.Println("Code exchange failed with '%s'\n", err)
 http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
 return
 }
 response, err := http.Get("https://www.googleapis.com/oauth2/v2/userinfo?access_token=" + token.AccessToken)
 defer response.Body.Close()
 contents, err := ioutil.ReadAll(response.Body)
 fmt.Fprintf(w, "Content: %s\n", contents)
}

把ClientID和ClientSecret换成你自己的!!!

浏览器访问:http://localhost:8000

点击Log in with Google

结果:

Content: {
 "id": "114512230444013345330",
 "email": "wangshubo1989@126.com",
 "verified_email": true,
 "name": "王书博",
 "given_name": "书博",
 "family_name": "王",
 "picture": "https://lh3.googleusercontent.com/-XdUIqdMkCWA/AAAAAAAAAAI/AAAAAAAAAAA/4252rscbv5M/photo.jpg",
 "locale": "zh-CN"
}

这里写图片描述


有疑问加站长微信联系(非本文作者)

本文来自:CSDN博客

感谢作者:wangshubo1989

查看原文:Go实战--golang中OAuth2.0的使用(使用google账号进行登陆验证)

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

关注微信
11105 次点击
暂无回复
添加一条新回复 (您需要 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传

用户登录

没有账号?注册
(追記) (追記ここまで)

今日阅读排行

    加载中
(追記) (追記ここまで)

一周阅读排行

    加载中

关注我

  • 扫码关注领全套学习资料 关注微信公众号
  • 加入 QQ 群:
    • 192706294(已满)
    • 731990104(已满)
    • 798786647(已满)
    • 729884609(已满)
    • 977810755(已满)
    • 815126783(已满)
    • 812540095(已满)
    • 1006366459(已满)
    • 692541889

  • 关注微信公众号
  • 加入微信群:liuxiaoyan-s,备注入群
  • 也欢迎加入知识星球 Go粉丝们(免费)

给该专栏投稿 写篇新文章

每篇文章有总共有 5 次投稿机会

收入到我管理的专栏 新建专栏