Go 每日一庫之 goth

簡介

當前很多網站直接採用第三方認證登錄,例如支付寶 / 微信 / 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】,共同學習,一起進步~

本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源https://mp.weixin.qq.com/s/juck9N2w_zJh1-RGnqSg7w