8 Star 0 Fork 0

YPT_ARCH / usercenter

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
client.go 2.95 KB
一键复制 编辑 原始数据 按行查看 历史
a526757124 提交于 2017-06-05 11:43 . Squashed commit of the following:
package main
import (
"encoding/json"
"log"
"net/http"
"golang.org/x/oauth2"
"fmt"
"git.oschina.net/YPTArch/basic/libs/remote"
"net/url"
)
var (
config1 = oauth2.Config{
ClientID: "dsfadafsdsfads1213fdsafdasfa",
ClientSecret: "dfas135wef1fa1fe5wf1ewfa12fwef",
Scopes: []string{"all"},
RedirectURL: "http://192.168.1.100:9094/oauth2",
Endpoint: oauth2.Endpoint{
AuthURL: "http://192.168.1.100:8080/usercenter/authorize",
TokenURL: "http://192.168.1.100:8080/usercenter/token",
},
}
)
func main() {
http.HandleFunc("/login", func(w http.ResponseWriter, r *http.Request) {
u := config1.AuthCodeURL("xyz")
fmt.Println(u)
http.Redirect(w, r, u, http.StatusFound)
})
http.HandleFunc("/codetoken", func(w http.ResponseWriter, r *http.Request) {
u:=config1.Endpoint.AuthURL+"?client_id="+config1.ClientID+"&client_secret="+config1.ClientSecret+"&response_type=token"+"&redirect_uri="+config1.RedirectURL
fmt.Println(u)
http.Redirect(w,r,u, http.StatusFound)
})
http.HandleFunc("/oauth2", func(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
state := r.Form.Get("state")
if state != "xyz" {
http.Error(w, "State invalid", http.StatusBadRequest)
return
}
code := r.Form.Get("code")
fmt.Println(code)
if code == "" {
http.Error(w, "Code not found", http.StatusBadRequest)
return
}
fmt.Println("--------------------------------------")
u:=config1.Endpoint.TokenURL+"?client_id="+config1.ClientID+"&client_secret="+config1.ClientSecret+"&grant_type=authorization_code"+"&redirect_uri="+config1.RedirectURL+"&code="+code+"&state="+ state
fmt.Println(u)
token, _, _, err := remote.HttpGet(u)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
e := json.NewEncoder(w)
e.SetIndent("", " ")
e.Encode(&token)
})
http.HandleFunc("/token", func(w http.ResponseWriter, r *http.Request) {
u:=config1.Endpoint.TokenURL+"?client_id="+config1.ClientID+"&client_secret="+config1.ClientSecret+"&grant_type=password"+"&redirect_uri="+config1.RedirectURL+"&username=13541085451&password=123456&scope=all"
fmt.Println(u)
token, _, _, err := remote.HttpGet(u)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
e := json.NewEncoder(w)
e.SetIndent("", " ")
e.Encode(&token)
})
http.HandleFunc("/refreshtoken", func(w http.ResponseWriter, r *http.Request) {
refreshToken:= r.URL.Query().Get("refresh_token")
refreshToken=url.QueryEscape(refreshToken)
u:=config1.Endpoint.TokenURL+"?client_id="+config1.ClientID+"&client_secret="+config1.ClientSecret+"&grant_type=refresh_token"+"&refresh_token="+refreshToken
fmt.Println(u)
token, _, _, err := remote.HttpGet(u)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
e := json.NewEncoder(w)
e.SetIndent("", " ")
e.Encode(&token)
})
log.Println("Client is running at 9094 port.")
log.Fatal(http.ListenAndServe(":9094", nil))
}
Go
1
https://gitee.com/YPTArch/usercenter.git
git@gitee.com:YPTArch/usercenter.git
YPTArch
usercenter
usercenter
master

搜索帮助

53164aa7 5694891 3bd8fe86 5694891