diff --git a/cmd/rdpgw/api/basic.go b/cmd/rdpgw/api/basic.go
index 91f6d172110c3691b5afea15cf0c31843e852705..afa4108d5dd6affa1ec5f2a2b2901a1c19626bd2 100644
--- a/cmd/rdpgw/api/basic.go
+++ b/cmd/rdpgw/api/basic.go
@@ -47,6 +47,8 @@ func (c *Config) BasicAuth(next http.HandlerFunc) http.HandlerFunc {
 			if !res.Authenticated {
 				log.Printf("User %s is not authenticated for this service", username)
 			} else {
+				ctx := context.WithValue(r.Context(), "preferred_username", username)
+				ctx = context.WithValue(ctx, "access_token", "EMPTY")
 				next.ServeHTTP(w, r.WithContext(ctx))
 				return
 			}
diff --git a/cmd/rdpgw/main.go b/cmd/rdpgw/main.go
index 24e4bb09bdf4c6cdeca66ee43417ec7ece4a138d..2800cf78fc7f9b975450a8c5fa8599275a0c95aa 100644
--- a/cmd/rdpgw/main.go
+++ b/cmd/rdpgw/main.go
@@ -42,37 +42,8 @@ func main() {
 	security.UserSigningKey = []byte(conf.Security.UserTokenSigningKey)
 	security.QuerySigningKey = []byte(conf.Security.QueryTokenSigningKey)
 
-	// set oidc config
-	provider, err := oidc.NewProvider(context.Background(), conf.OpenId.ProviderUrl)
-	if err != nil {
-		log.Fatalf("Cannot get oidc provider: %s", err)
-	}
-	oidcConfig := &oidc.Config{
-		ClientID: conf.OpenId.ClientId,
-	}
-	verifier := provider.Verifier(oidcConfig)
-
-	// get callback url and external advertised gateway address
-	url, err := url.Parse(conf.Server.GatewayAddress)
-	if url.Scheme == "" {
-		url.Scheme = "https"
-	}
-	url.Path = "callback"
-
-	oauthConfig := oauth2.Config{
-		ClientID:     conf.OpenId.ClientId,
-		ClientSecret: conf.OpenId.ClientSecret,
-		RedirectURL:  url.String(),
-		Endpoint:     provider.Endpoint(),
-		Scopes:       []string{oidc.ScopeOpenID, "profile", "email"},
-	}
-	security.OIDCProvider = provider
-	security.Oauth2Config = oauthConfig
-
+	// configure api
 	api := &api.Config{
-		GatewayAddress:       url.Host,
-		OAuth2Config:         &oauthConfig,
-		OIDCTokenVerifier:    verifier,
 		PAATokenGenerator:    security.GeneratePAAToken,
 		UserTokenGenerator:   security.GenerateUserToken,
 		QueryInfo:            security.QueryInfo,
@@ -92,6 +63,38 @@ func main() {
 		SocketAddress:        conf.Server.AuthSocket,
 		Authentication:       conf.Server.Authentication,
 	}
+
+	if conf.Server.Authentication == "openid" {
+		// set oidc config
+		provider, err := oidc.NewProvider(context.Background(), conf.OpenId.ProviderUrl)
+		if err != nil {
+			log.Fatalf("Cannot get oidc provider: %s", err)
+		}
+		oidcConfig := &oidc.Config{
+			ClientID: conf.OpenId.ClientId,
+		}
+		verifier := provider.Verifier(oidcConfig)
+
+		// get callback url and external advertised gateway address
+		url, err := url.Parse(conf.Server.GatewayAddress)
+		if url.Scheme == "" {
+			url.Scheme = "https"
+		}
+		url.Path = "callback"
+		api.GatewayAddress = url.Host
+
+		oauthConfig := oauth2.Config{
+			ClientID:     conf.OpenId.ClientId,
+			ClientSecret: conf.OpenId.ClientSecret,
+			RedirectURL:  url.String(),
+			Endpoint:     provider.Endpoint(),
+			Scopes:       []string{oidc.ScopeOpenID, "profile", "email"},
+		}
+		security.OIDCProvider = provider
+		security.Oauth2Config = oauthConfig
+		api.OAuth2Config = &oauthConfig
+		api.OIDCTokenVerifier = verifier
+	}
 	api.NewApi()
 
 	log.Printf("Starting remote desktop gateway server")
diff --git a/cmd/rdpgw/security/jwt.go b/cmd/rdpgw/security/jwt.go
index 8deef420c7abfad825633a73a46140664dc54701..84ab15b5949bb38116b8a64b3b05254827a18cda 100644
--- a/cmd/rdpgw/security/jwt.go
+++ b/cmd/rdpgw/security/jwt.go
@@ -65,11 +65,13 @@ func VerifyPAAToken(ctx context.Context, tokenString string) (bool, error) {
 	}
 
 	// validate the access token
-	tokenSource := Oauth2Config.TokenSource(ctx, &oauth2.Token{AccessToken: custom.AccessToken})
-	_, err = OIDCProvider.UserInfo(ctx, tokenSource)
-	if err != nil {
-		log.Printf("Cannot get user info for access token: %s", err)
-		return false, err
+	if custom.AccessToken != "EMPTY" {
+		tokenSource := Oauth2Config.TokenSource(ctx, &oauth2.Token{AccessToken: custom.AccessToken})
+		_, err = OIDCProvider.UserInfo(ctx, tokenSource)
+		if err != nil {
+			log.Printf("Cannot get user info for access token: %s", err)
+			return false, err
+		}
 	}
 
 	s := getSessionInfo(ctx)
diff --git a/cmd/rdpgw/security/string.go b/cmd/rdpgw/security/string.go
index dcf5821c357a464f68daf71d15491d3f23258238..a79dbffabb03ba65e1555bc119cbb608a54d63bc 100644
--- a/cmd/rdpgw/security/string.go
+++ b/cmd/rdpgw/security/string.go
@@ -32,7 +32,7 @@ func GenerateRandomString(n int) (string, error) {
 		if err != nil {
 			return "", err
 		}
-		ret = append(ret, letters[num.Int64()])
+		ret[i] = letters[num.Int64()]
 	}
 
 	return string(ret), nil