-
Notifications
You must be signed in to change notification settings - Fork 501
/
driver.go
63 lines (53 loc) · 1.54 KB
/
driver.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package azuread
import (
"context"
"database/sql"
"database/sql/driver"
mssql "github.com/denisenkom/go-mssqldb"
)
// DriverName is the name used to register the driver
const DriverName = "azuresql"
func init() {
sql.Register(DriverName, &Driver{})
}
// Driver wraps the underlying MSSQL driver, but configures the Azure AD token provider
type Driver struct {
}
// Open returns a new connection to the database.
func (d *Driver) Open(dsn string) (driver.Conn, error) {
c, err := NewConnector(dsn)
if err != nil {
return nil, err
}
return c.Connect(context.Background())
}
// NewConnector creates a new connector from a DSN.
// The returned connector may be used with sql.OpenDB.
func NewConnector(dsn string) (*mssql.Connector, error) {
config, err := parse(dsn)
if err != nil {
return nil, err
}
return newConnectorConfig(config)
}
// newConnectorConfig creates a Connector from config.
func newConnectorConfig(config *azureFedAuthConfig) (*mssql.Connector, error) {
switch config.fedAuthLibrary {
case mssql.FedAuthLibraryADAL:
return mssql.NewActiveDirectoryTokenConnector(
config.mssqlConfig, config.adalWorkflow,
func(ctx context.Context, serverSPN, stsURL string) (string, error) {
return config.provideActiveDirectoryToken(ctx, serverSPN, stsURL)
},
)
case mssql.FedAuthLibrarySecurityToken:
return mssql.NewSecurityTokenConnector(
config.mssqlConfig,
func(ctx context.Context) (string, error) {
return config.password, nil
},
)
default:
return mssql.NewConnectorConfig(config.mssqlConfig), nil
}
}