Documentation
¶
Overview ¶
Package dingtalk implements the OAuth2 protocol for authenticating users through DingTalk. This package can be used as a reference implementation of an OAuth2 provider for Goth.
Configuration ¶
To use the DingTalk provider, you need to create an application in the DingTalk Open Platform (https://open.dingtalk.com/):
- Register a corporate/organization application to get AppKey and AppSecret
- Set callback URL: http://your-domain/auth/dingtalk/callback
- Request these necessary API permissions: - Contact.User.Read (必须/Required) - Contact.Member.Read (必须/Required)
Example ¶
// Basic use:
dingTalkProvider := dingtalk.New(
os.Getenv("DINGTALK_KEY"),
os.Getenv("DINGTALK_SECRET"),
"http://localhost:3000/auth/dingtalk/callback",
"", // empty string if you don't need corporate ID verification
"openid" // minimum scope
)
// With corporate verification (limit to specific company):
dingTalkProvider := dingtalk.New(
os.Getenv("DINGTALK_KEY"),
os.Getenv("DINGTALK_SECRET"),
"http://localhost:3000/auth/dingtalk/callback",
os.Getenv("DINGTALK_CORP_ID"), // corporate ID for verification
"openid",
"corpid" // needed for corporate verification
)
// Enable debug mode for detailed logging
dingTalkProvider.Debug(true)
goth.UseProviders(dingTalkProvider)
Environment Variables ¶
DINGTALK_KEY: Your DingTalk application's client key/app key DINGTALK_SECRET: Your DingTalk application's client secret/app secret DINGTALK_CORP_ID: (Optional) For corporate ID verification, to limit authentication to a specific company
See the examples/main.go file for a working example of this provider.
Index ¶
- Variables
- func GetCorpID(user goth.User) (string, bool)
- type Provider
- func (p *Provider) BeginAuth(state string) (goth.Session, error)
- func (p *Provider) Client() *http.Client
- func (p *Provider) Debug(debug bool)
- func (p *Provider) FetchUser(session goth.Session) (goth.User, error)
- func (p *Provider) Name() string
- func (p *Provider) RefreshToken(refreshToken string) (*oauth2.Token, error)
- func (p *Provider) RefreshTokenAvailable() bool
- func (p *Provider) SetName(name string)
- func (p *Provider) UnmarshalSession(data string) (goth.Session, error)
- type Session
Constants ¶
This section is empty.
Variables ¶
var ( AuthURL = "https://login.dingtalk.com/oauth2/auth" TokenURL = "https://api.dingtalk.com/v1.0/oauth2/userAccessToken" ProfileURL = "https://api.dingtalk.com/v1.0/contact/users/me" )
These vars define the Authentication, Token, and API URLS for DingTalk. See: https://open.dingtalk.com/document/orgapp/tutorial-obtaining-user-personal-information
Functions ¶
Types ¶
type Provider ¶
type Provider struct {
ClientKey string
Secret string
CallbackURL string
HTTPClient *http.Client
// contains filtered or unexported fields
}
Provider is the implementation of `goth.Provider` for accessing DingTalk.
func New ¶
New creates a new DingTalk provider, and sets up important connection details. You should always call `dingtalk.New` to get a new Provider. Never try to create one manually.
When using with "corpid" scope, include "openid" and "corpid" in scopes parameter.
func NewCustomisedURL ¶
func NewCustomisedURL(clientKey, secret, callbackURL, authURL, tokenURL, profileURL, expectedCorpID string, scopes ...string) *Provider
NewCustomisedURL is similar to New(...) but can be used to set custom URLs to connect to If expectedCorpID is non-empty, the provider will verify that authenticated users belong to the specified company.
func NewWithCorpID ¶
func NewWithCorpID(clientKey, secret, callbackURL, expectedCorpID string, scopes ...string) *Provider
NewWithCorpID creates a new DingTalk provider with company ID verification. If expectedCorpID is non-empty, the provider will verify that authenticated users belong to the specified company. Authentication will fail if the user's corpID doesn't match.
Use this constructor when you need to restrict access to users from a specific company. Be sure to include "openid" and "corpid" in the scopes parameter.
func (*Provider) FetchUser ¶
FetchUser will go to DingTalk and access basic information about the user. If expectedCorpID is set and the user's corpID doesn't match, an error will be returned.
func (*Provider) RefreshToken ¶
RefreshToken get new access token based on the refresh token
func (*Provider) RefreshTokenAvailable ¶
RefreshTokenAvailable refresh token is provided by DingTalk
type Session ¶
type Session struct {
AuthURL string
AccessToken string
RefreshToken string
ExpiresAt time.Time
CorpID string // Corporate ID of the authenticated user
ExpectedCorpID string // Expected Corporate ID for validation
}
Session stores data during the auth process with DingTalk.
func (*Session) Authorize ¶
Authorize the session with DingTalk and return the access token to be stored for future use.
func (Session) GetAuthURL ¶
GetAuthURL will return the URL set by calling the `BeginAuth` function on the DingTalk provider.