-
Notifications
You must be signed in to change notification settings - Fork 0
/
oAuth.go
44 lines (35 loc) · 968 Bytes
/
oAuth.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
package auth
import (
"context"
"fmt"
"net/http"
"golang.org/x/oauth2/clientcredentials"
)
/*
*/
// OAuthConfig is a struct that contains the OAuth2 configuration
type OAuthConfig struct {
ClientID string
ClientSecret string
TokenURL string
Scopes []string
}
// OAuthAuth performs authentication using OAuth2 with a generic provider
func OAuthAuth(w http.ResponseWriter, r *http.Request, config *OAuthConfig) {
// Create the OAuth2 config
oauthConfig := &clientcredentials.Config{
ClientID: config.ClientID,
ClientSecret: config.ClientSecret,
TokenURL: config.TokenURL,
Scopes: config.Scopes,
}
// Get the token
token, err := oauthConfig.Token(context.Background())
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Perform any additional actions here, such as storing the
// user's information in a database
fmt.Fprintln(w, "Access Token:", token.AccessToken)
}