| 1 |
package providers |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"fmt" |
| 6 |
"net/http" |
| 7 |
|
| 8 |
"github.com/jkroepke/openvpn-auth-oauth2/v2/internal/config" |
| 9 |
"github.com/jkroepke/openvpn-auth-oauth2/v2/internal/oauth2" |
| 10 |
"github.com/jkroepke/openvpn-auth-oauth2/v2/internal/oauth2/providers/generic" |
| 11 |
"github.com/jkroepke/openvpn-auth-oauth2/v2/internal/oauth2/providers/github" |
| 12 |
"github.com/jkroepke/openvpn-auth-oauth2/v2/internal/oauth2/providers/google" |
| 13 |
) |
| 14 |
|
| 15 |
// New creates the OAuth2 provider implementation selected by configuration. |
| 16 |
func New(ctx context.Context, conf *config.Config, httpClient *http.Client) (oauth2.Provider, error) { |
| 17 |
switch conf.OAuth2.Provider { |
| 18 |
case generic.Name: |
| 19 |
provider, err := generic.NewProvider(ctx, conf, httpClient) |
| 20 |
if err != nil { |
| 21 |
return nil, fmt.Errorf("generic provider: %w", err) |
| 22 |
} |
| 23 |
|
| 24 |
return provider, nil |
| 25 |
case github.Name: |
| 26 |
provider, err := github.NewProvider(ctx, conf, httpClient) |
| 27 |
if err != nil { |
| 28 |
return nil, fmt.Errorf("github provider: %w", err) |
| 29 |
} |
| 30 |
|
| 31 |
return provider, nil |
| 32 |
case google.Name: |
| 33 |
provider, err := google.NewProvider(ctx, conf, httpClient) |
| 34 |
if err != nil { |
| 35 |
return nil, fmt.Errorf("google provider: %w", err) |
| 36 |
} |
| 37 |
|
| 38 |
return provider, nil |
| 39 |
default: |
| 40 |
return nil, fmt.Errorf("unknown oauth2 provider: %s", conf.OAuth2.Provider) |
| 41 |
} |
| 42 |
} |