关于golang:Go-每日一库之-goth

5次阅读

共计 3824 个字符,预计需要花费 10 分钟才能阅读完成。

简介

以后很多网站间接采纳第三方认证登录,例如支付宝 / 微信 / Github 等。goth封装了接入第三方认证的办法,并且内置实现了很多第三方认证的实现:

图中截取的只是 goth 反对的一部分,残缺列表可在其 GitHub 首页查看。

疾速应用

本文代码应用 Go Modules。

创立目录并初始化:

$ mkdir goth && cd goth
$ go mod init github.com/darjun/go-daily-lib/goth

装置 goth 库:

$ go get -u github.com/markbates/goth

咱们设计了两个页面,一个登录页面:

// login.tpl
<a href="/auth/github?provider=github">Login With GitHub</a>

点击登录链接会申请/auth/github?provider=github

一个主界面:

// home.tpl
<p><a href="/logout/github">logout</a></p>
<p>Name: {{.Name}} [{{.LastName}}, {{.FirstName}}]</p>
<p>Email: {{.Email}}</p>
<p>NickName: {{.NickName}}</p>
<p>Location: {{.Location}}</p>
<p>AvatarURL: {{.AvatarURL}} <img src="{{.AvatarURL}}"></p>
<p>Description: {{.Description}}</p>
<p>UserID: {{.UserID}}</p>
<p>AccessToken: {{.AccessToken}}</p>
<p>ExpiresAt: {{.ExpiresAt}}</p>
<p>RefreshToken: {{.RefreshToken}}</p>

显示用户的根本信息。

同样地,咱们应用 html/template 规范模板库来加载和治理页面模板:

var (ptTemplate *template.Template)

func init() {ptTemplate = template.Must(template.New("").ParseGlob("tpls/*.tpl"))
}

主页面解决如下:

func HomeHandler(w http.ResponseWriter, r *http.Request) {user, err := gothic.CompleteUserAuth(w, r)
  if err != nil {http.Redirect(w, r, "/login/github", http.StatusTemporaryRedirect)
    return
  }
  ptTemplate.ExecuteTemplate(w, "home.tpl", user)
}

如果用户登录了,gothic.CompleteUserAuth(w, r)会返回一个非空的 User 对象,该类型有如下字段:

type User struct {RawData           map[string]interface{}
  Provider          string
  Email             string
  Name              string
  FirstName         string
  LastName          string
  NickName          string
  Description       string
  UserID            string
  AvatarURL         string
  Location          string
  AccessToken       string
  AccessTokenSecret string
  RefreshToken      string
  ExpiresAt         time.Time
  IDToken           string
}

如果已登录,显示主界面信息。如果未登录,重定向到登录界面:

func LoginHandler(w http.ResponseWriter, r *http.Request) {ptTemplate.ExecuteTemplate(w, "login.tpl", nil)
}

点击登录,由 AuthHandler 解决申请:

func AuthHandler(w http.ResponseWriter, r *http.Request) {gothic.BeginAuthHandler(w, r)
}

调用 gothic.BeginAuthHandler(w, r) 开始跳转到 GitHub 的验证界面。GitHub 验证实现后,浏览器会重定向到 /auth/github/callback 解决:

func CallbackHandler(w http.ResponseWriter, r *http.Request) {user, err := gothic.CompleteUserAuth(w, r)
  if err != nil {fmt.Fprintln(w, err)
    return
  }
  ptTemplate.ExecuteTemplate(w, "home.tpl", user)
}

如果登录胜利,在 CallbackHandler 中,咱们能够调用 gothic.CompleteUserAuth(w, r) 取出 User 对象,而后显示主页面。最初是音讯路由设置:

r := mux.NewRouter()
r.HandleFunc("/", HomeHandler)
r.HandleFunc("/login/github", LoginHandler)
r.HandleFunc("/logout/github", LogoutHandler)
r.HandleFunc("/auth/github", AuthHandler)
r.HandleFunc("/auth/github/callback", CallbackHandler)

log.Println("listening on localhost:8080")
log.Fatal(http.ListenAndServe(":8080", r))

goth为咱们封装了 GitHub 的验证过程,然而咱们须要在 GitHub 上新增一个 OAuth App,生成 Client ID 和 Client Secret。

首先,登录 GitHub 账号,在右侧头像下拉框抉择 Settings:

抉择左侧 Developer Settings:

左侧抉择 OAuth App,右侧点击 New OAuth App:

输出信息,重点是Authorization callback URL,这是 GitHub 验证胜利之后的回调:

生成 App 之后,Client ID 会主动生成,然而 Client Secret 须要再点击右侧的按钮 Generate a new client token 生成:

生成了 Client Secret:

想要在程序中应用 Github,首先要创立一个 GitHub 的 Provider,调用 github 子包的 New() 办法:

githubProvider := github.New(clientKey, clientSecret, "http://localhost:8080/auth/github/callback")

第一个参数为 Client ID,第二个参数为 Client Secret,这两个是由下面的 OAuth App 生成的,第三个参数为回调的链接,这个必须与 OAuth App 创立时设置的一样。

而后利用这个 Provider:

goth.UseProviders(githubProvider)

筹备工作实现,长吁一口气。当初运行程序:

$ SECRET_KEY="secret" go run main.go

浏览器拜访localhost:8080,因为没有登录,重定向到localhost:8080/login/github

点击Login with GitHub,会重定向到 GitHub 受权页面:

点击受权,胜利之后用户信息会保留在 session
中。跳转到主页面,显示我的信息:

更换 store

goth底层应用上一篇文章中介绍的 gorilla/sessions 库来存储登录信息,而默认采纳的是 cookie 作为存储。另外选项默认采纳:

&Options{
  Path:   "/",
  Domain: "",
  MaxAge: 86400 * 30,
  HttpOnly: true,
  Secure: false,
}

如果须要更改存储形式或选项,咱们能够在程序启动前,设置 gothic.Store 字段。例如咱们要更换为 redistore:

store, _ = redistore.NewRediStore(10, "tcp", ":6379", "", []byte("redis-key"))

key := ""
maxAge := 86400 * 30  // 30 days
isProd := false

store := sessions.NewCookieStore([]byte(key))
store.MaxAge(maxAge)
store.Options.Path = "/"
store.Options.HttpOnly = true
store.Options.Secure = isProd

gothic.Store = store

总结

大家如果发现好玩、好用的 Go 语言库,欢送到 Go 每日一库 GitHub 上提交 issue😄

参考

  1. goth GitHub:github.com/markbates/goth
  2. Go 每日一库 GitHub:https://github.com/darjun/go-daily-lib

我的博客:https://darjun.github.io

欢送关注我的微信公众号【GoUpUp】,独特学习,一起提高~

正文完
 0