Skip to content

Commit

Permalink
oauth
Browse files Browse the repository at this point in the history
  • Loading branch information
Ceelog committed Sep 8, 2020
1 parent 24072c3 commit b47f705
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 108 deletions.
91 changes: 91 additions & 0 deletions corporation/apis/oauth/oauth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Copyright 2020 FastWeGo
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package oauth 网页授权登录(oauth)

package oauth

import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
)

const (
apiAuthorize = "https://open.weixin.qq.com/connect/oauth2/authorize"
apiUserInfo = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo"
)

/*
构造网页授权链接
如果用户同意授权,页面将跳转至 redirect_uri/?code=CODE&state=STATE
See: https://work.weixin.qq.com/api/doc/90000/90135/91022
GET https://open.weixin.qq.com/connect/oauth2/authorize?appid=CORPID&redirect_uri=REDIRECT_URI&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect
*/
func GetAuthorizeUrl(appid string, redirectUri string, state string) (authorizeUrl string) {
params := url.Values{}
params.Add("appid", appid)
params.Add("redirect_uri", redirectUri)
params.Add("response_type", "code")
params.Add("scope", "snsapi_base")
params.Add("state", state)
return apiAuthorize + "?" + params.Encode() + "#wechat_redirect"
}

type UserInfo struct {
Errcode int `json:"errcode"`
Errmsg string `json:"errmsg"`
UserID string `json:"UserId"`
DeviceID string `json:"DeviceId"`
}

/*
获取访问用户身份
该接口用于根据code获取成员信息
See: https://work.weixin.qq.com/api/doc/90000/90135/91023
GET https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token=ACCESS_TOKEN&code=CODE
*/
func GetUserInfo(accessToken string, code string) (userInfo UserInfo, err error) {
params := url.Values{}
params.Add("access_token", accessToken)
params.Add("code", code)

uri := apiUserInfo + "?" + params.Encode()
response, err := http.Get(uri)
if err != nil {
return
}

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

err = json.Unmarshal(body, &userInfo)
if err != nil {
err = fmt.Errorf("%s", string(body))
return
}

return
}
31 changes: 31 additions & 0 deletions corporation/cmd/apiconfig_crop.go
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,37 @@ var apiConfigCrop = []ApiGroup{
},
},
},
{
Name: `身份验证`,
Package: `oauth`,
Apis: []Api{
{
Name: "构造网页授权链接",
Description: "如果企业需要在打开的网页里面携带用户的身份信息,第一步需要构造如下的链接来获取code参数 ",
Request: "GET https://open.weixin.qq.com/connect/oauth2/authorize?appid=CORPID&redirect_uri=REDIRECT_URI&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect",
See: "https://work.weixin.qq.com/api/doc/90000/90135/91022",
FuncName: "GetAuthorizeUrl",
GetParams: []Param{
{Name: `appid`, Type: `string`},
{Name: `redirect_uri`, Type: `string`},
{Name: `response_type`, Type: `string`},
{Name: `scope`, Type: `string`},
{Name: `state`, Type: `string`},
},
},
{
Name: "获取访问用户身份",
Description: "该接口用于根据code获取成员信息",
Request: "GET https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token=ACCESS_TOKEN&code=CODE",
See: "https://work.weixin.qq.com/api/doc/90000/90135/91023",
FuncName: "GetUserInfo",
GetParams: []Param{
{Name: `access_token`, Type: `string`},
{Name: `code`, Type: `string`},
},
},
},
},
{
Name: `身份验证`,
Package: `verify`,
Expand Down
5 changes: 5 additions & 0 deletions corporation/doc/apilist.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@
- [GetUserBehaviorData (/cgi-bin/externalcontact/get_user_behavior_data)](https://pkg.go.dev/github.com/fastwego/wxwork/corporation/apis/externalcontact?tab=doc#GetUserBehaviorData)
- [获取客户群统计数据](https://work.weixin.qq.com/api/doc/90000/90135/92133)
- [GroupchatStatistic (/cgi-bin/externalcontact/groupchat/statistic)](https://pkg.go.dev/github.com/fastwego/wxwork/corporation/apis/externalcontact?tab=doc#GroupchatStatistic)
- 身份验证(oauth)
- [构造网页授权链接](https://work.weixin.qq.com/api/doc/90000/90135/91022)
- [GetAuthorizeUrl (/connect/oauth2/authorize)](https://pkg.go.dev/github.com/fastwego/wxwork/corporation/apis/oauth?tab=doc#GetAuthorizeUrl)
- [获取访问用户身份](https://work.weixin.qq.com/api/doc/90000/90135/91023)
- [GetUserInfo (/cgi-bin/user/getuserinfo)](https://pkg.go.dev/github.com/fastwego/wxwork/corporation/apis/oauth?tab=doc#GetUserInfo)
- 身份验证(verify)
- [获取访问用户身份](https://work.weixin.qq.com/api/doc/90000/90135/91023)
- [GetUserInfo (/cgi-bin/user/getuserinfo)](https://pkg.go.dev/github.com/fastwego/wxwork/corporation/apis/verify?tab=doc#GetUserInfo)
Expand Down
2 changes: 1 addition & 1 deletion corporation/example_corporation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"github.com/faabiosr/cachego/file"
"github.com/faabiosr/cachego/sync"
"github.com/fastwego/wxwork/corporation"
"github.com/garyburd/redigo/redis"
"github.com/gomodule/redigo/redis"
)

func ExampleCorporation_SetAccessTokenCacheDriver() {
Expand Down
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ go 1.14
require (
github.com/PuerkitoBio/goquery v1.5.1
github.com/faabiosr/cachego v0.15.0
github.com/garyburd/redigo v1.6.0
github.com/gomodule/redigo v1.8.2
github.com/iancoleman/strcase v0.0.0-20191112232945-16388991a334
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208 // indirect
)
Loading

0 comments on commit b47f705

Please sign in to comment.