mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-11-27 09:11:53 -05:00
6e23a1b843
update go-mssqldb 2019-11-28 (1d7a30a10f73) -> 2020-04-28 (06a60b6afbbc)
51 lines
1.3 KiB
Go
Vendored
51 lines
1.3 KiB
Go
Vendored
// +build go1.10
|
|
|
|
package mssql
|
|
|
|
import (
|
|
"context"
|
|
"database/sql/driver"
|
|
"errors"
|
|
"fmt"
|
|
)
|
|
|
|
var _ driver.Connector = &accessTokenConnector{}
|
|
|
|
// accessTokenConnector wraps Connector and injects a
|
|
// fresh access token when connecting to the database
|
|
type accessTokenConnector struct {
|
|
Connector
|
|
|
|
accessTokenProvider func() (string, error)
|
|
}
|
|
|
|
// 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
|
|
}
|
|
|
|
c := &accessTokenConnector{
|
|
Connector: *conn,
|
|
accessTokenProvider: tokenProvider,
|
|
}
|
|
return c, nil
|
|
}
|
|
|
|
// Connect returns a new database connection
|
|
func (c *accessTokenConnector) Connect(ctx context.Context) (driver.Conn, error) {
|
|
var err error
|
|
c.Connector.params.fedAuthAccessToken, err = c.accessTokenProvider()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("mssql: error retrieving access token: %+v", err)
|
|
}
|
|
|
|
return c.Connector.Connect(ctx)
|
|
}
|