2020-05-10 11:42:52 -06:00
|
|
|
// +build go1.10
|
|
|
|
|
|
|
|
package mssql
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql/driver"
|
|
|
|
"errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewAccessTokenConnector creates a new connector from a DSN and a token provider.
|
|
|
|
// The token provider func will be called when a new connection is requested and should return a valid access token.
|
|
|
|
// The returned connector may be used with sql.OpenDB.
|
|
|
|
func NewAccessTokenConnector(dsn string, tokenProvider func() (string, error)) (driver.Connector, error) {
|
|
|
|
if tokenProvider == nil {
|
|
|
|
return nil, errors.New("mssql: tokenProvider cannot be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
conn, err := NewConnector(dsn)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-08-28 05:15:21 -06:00
|
|
|
conn.params.fedAuthLibrary = fedAuthLibrarySecurityToken
|
|
|
|
conn.securityTokenProvider = func(ctx context.Context) (string, error) {
|
|
|
|
return tokenProvider()
|
2020-05-10 11:42:52 -06:00
|
|
|
}
|
|
|
|
|
2021-08-28 05:15:21 -06:00
|
|
|
return conn, nil
|
2020-05-10 11:42:52 -06:00
|
|
|
}
|