forked from mitch-strong/KeycloakGo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeycloak.go
52 lines (43 loc) · 1.56 KB
/
keycloak.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
package keycloak
import (
"context"
"fmt"
oidc "github.com/coreos/go-oidc"
"golang.org/x/oauth2"
)
//Constants
const keycloakJSONFileName = "keycloak.json"
//Global Variables
var client Client //Client Object
var realm string //realm string from json file
var clientID string //Client ID string from json file
var clientSecret string //Client ID sectret from json file
var oauth2Config oauth2.Config //oath2Config
var provider *oidc.Provider //oidc provider
var err error //generic error object
var keycloakserver string //keycloak server string passed from app
var server string //app server string passed from app
var verifier *oidc.IDTokenVerifier //verifier
//Init begins keycloak server
func Init(keycloakServer, Server string) {
userLog = GetInstance()
getKeycloakJSON()
keycloakserver = keycloakServer
server = Server
ctx := context.Background()
//Gets the provider for authentication (keycloak)
provider, err = oidc.NewProvider(ctx, keycloakserver+"/auth/realms/"+realm)
if err != nil {
fmt.Printf("This is an error with regard to the context: %v", err)
}
verifier = provider.Verifier(&oidc.Config{ClientID: clientID})
// Configure an OpenID Connect aware OAuth2 client.
oauth2Config = oauth2.Config{
ClientID: clientID,
ClientSecret: clientSecret,
RedirectURL: server + "/loginCallback",
// Discovery returns the OAuth2 endpoints.
Endpoint: provider.Endpoint(),
Scopes: []string{oidc.ScopeOpenID, "profile", "email"},
}
}