dingtalk

package
v0.0.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 25, 2026 License: MIT Imports: 12 Imported by: 0

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/):

  1. Register a corporate/organization application to get AppKey and AppSecret
  2. Set callback URL: http://your-domain/auth/dingtalk/callback
  3. 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

Constants

This section is empty.

Variables

View Source
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

func GetCorpID

func GetCorpID(user goth.User) (string, bool)

GetCorpID retrieves the company ID from user data Returns the corpID and whether it was found

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

func New(clientKey, secret, callbackURL string, expectedCorpID string, scopes ...string) *Provider

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) BeginAuth

func (p *Provider) BeginAuth(state string) (goth.Session, error)

BeginAuth asks DingTalk for an authentication end-point.

func (*Provider) Client

func (p *Provider) Client() *http.Client

func (*Provider) Debug

func (p *Provider) Debug(debug bool)

Debug sets the debug mode

func (*Provider) FetchUser

func (p *Provider) FetchUser(session goth.Session) (goth.User, error)

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) Name

func (p *Provider) Name() string

Name is the name used to retrieve this provider later.

func (*Provider) RefreshToken

func (p *Provider) RefreshToken(refreshToken string) (*oauth2.Token, error)

RefreshToken get new access token based on the refresh token

func (*Provider) RefreshTokenAvailable

func (p *Provider) RefreshTokenAvailable() bool

RefreshTokenAvailable refresh token is provided by DingTalk

func (*Provider) SetName

func (p *Provider) SetName(name string)

SetName is to update the name of the provider (needed in case of multiple providers of 1 type)

func (*Provider) UnmarshalSession

func (p *Provider) UnmarshalSession(data string) (goth.Session, error)

UnmarshalSession will unmarshal a JSON string into a session.

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

func (s *Session) Authorize(provider goth.Provider, params goth.Params) (string, error)

Authorize the session with DingTalk and return the access token to be stored for future use.

func (Session) GetAuthURL

func (s Session) GetAuthURL() (string, error)

GetAuthURL will return the URL set by calling the `BeginAuth` function on the DingTalk provider.

func (Session) Marshal

func (s Session) Marshal() string

Marshal the session into a string

func (Session) String

func (s Session) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL