Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
K
keycloak-proxy
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Janne Mareike Koschinski
keycloak-proxy
Commits
b3a1eea4
Commit
b3a1eea4
authored
Feb 24, 2016
by
Rohith
Browse files
Options
Downloads
Plain Diff
Merge pull request #27 from gambol99/wip
Wip
parents
00629a68
3f41a6a4
No related branches found
No related tags found
No related merge requests found
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
config.go
+7
-0
7 additions, 0 deletions
config.go
doc.go
+2
-0
2 additions, 0 deletions
doc.go
server.go
+21
-1
21 additions, 1 deletion
server.go
server_test.go
+86
-52
86 additions, 52 deletions
server_test.go
with
116 additions
and
53 deletions
config.go
+
7
−
0
View file @
b3a1eea4
...
...
@@ -158,6 +158,9 @@ func readOptions(cx *cli.Context, config *Config) (err error) {
if
cx
.
IsSet
(
"encryption-key"
)
{
config
.
EncryptionKey
=
cx
.
String
(
"encryption-key"
)
}
if
cx
.
IsSet
(
"no-redirects"
)
{
config
.
NoRedirects
=
cx
.
Bool
(
"no-redirects"
)
}
if
cx
.
IsSet
(
"redirection-url"
)
{
config
.
RedirectionURL
=
cx
.
String
(
"redirection-url"
)
}
...
...
@@ -309,6 +312,10 @@ func getOptions() []cli.Flag {
Name
:
"encryption-key"
,
Usage
:
"the encryption key used to encrpytion the session state"
,
},
cli
.
BoolFlag
{
Name
:
"no-redirects"
,
Usage
:
"do not have back redirects when no authentication is present, simple reply with 401 code"
,
},
cli
.
StringFlag
{
Name
:
"redirection-url"
,
Usage
:
fmt
.
Sprintf
(
"the redirection url, namely the site url, note: %s will be added to it"
,
oauthURL
),
...
...
This diff is collapsed.
Click to expand it.
doc.go
+
2
−
0
View file @
b3a1eea4
...
...
@@ -95,6 +95,8 @@ type Config struct {
ClientID
string
`json:"clientid" yaml:"clientid"`
// Secret is the secret for AS
Secret
string
`json:"secret" yaml:"secret"`
// NoRedirects informs we should hand back a 401 not a redirect
NoRedirects
bool
`json:"no-redirects" yaml:"no-redirects"`
// RedirectionURL the redirection url
RedirectionURL
string
`json:"redirection_url" yaml:"redirection_url"`
// EnableSecurityFilter enabled the security handler
...
...
This diff is collapsed.
Click to expand it.
server.go
+
21
−
1
View file @
b3a1eea4
...
...
@@ -217,13 +217,19 @@ func (r KeycloakProxy) accessForbidden(cx *gin.Context) {
// redirectToAuthorization redirects the user to authorization handler
func
(
r
KeycloakProxy
)
redirectToAuthorization
(
cx
*
gin
.
Context
)
{
// step: are we handling redirects?
if
r
.
config
.
NoRedirects
{
cx
.
AbortWithStatus
(
http
.
StatusUnauthorized
)
return
}
// step: add a state referrer to the authorization page
authQuery
:=
fmt
.
Sprintf
(
"?state=%s"
,
cx
.
Request
.
URL
.
String
())
// step: if verification is switched off, we can't authorization
if
r
.
config
.
SkipTokenVerification
{
log
.
Errorf
(
"refusing to redirection to authorization endpoint, skip token verification switched on"
)
r
.
acces
sForbidden
(
cx
)
cx
.
AbortWithStatus
(
http
.
Statu
sForbidden
)
return
}
...
...
@@ -253,6 +259,20 @@ func (r *KeycloakProxy) injectCORSHeaders(cx *gin.Context) {
}
}
func
(
r
*
KeycloakProxy
)
addAuthenticationHeader
(
cx
*
gin
.
Context
,
errorCode
,
errorMessage
string
)
{
// step: inject the error message
header
:=
"Bearer realm=
\"
secure
\"
"
if
errorCode
!=
""
{
header
+=
fmt
.
Sprintf
(
",error=
\"
%s
\"
"
,
errorCode
)
}
if
errorMessage
!=
""
{
header
+=
fmt
.
Sprintf
(
", error_description=
\"
%s
\"
"
,
errorMessage
)
}
// step: add the www-authenticate header
cx
.
Writer
.
Header
()
.
Set
(
"WWW-Authenticate"
,
header
)
}
// tryUpdateConnection attempt to upgrade the connection to a http pdy stream
func
(
r
*
KeycloakProxy
)
tryUpdateConnection
(
cx
*
gin
.
Context
)
error
{
// step: dial the endpoint
...
...
This diff is collapsed.
Click to expand it.
server_test.go
+
86
−
52
View file @
b3a1eea4
...
...
@@ -20,11 +20,12 @@ import (
"io/ioutil"
"net"
"net/http"
"net/url"
"testing"
log
"github.com/Sirupsen/logrus"
"github.com/gin-gonic/gin"
"
net/url
"
"
github.com/stretchr/testify/assert
"
)
const
(
...
...
@@ -48,11 +49,8 @@ func newFakeKeycloakProxyWithResources(t *testing.T, resources []*Resource) *Key
return
kc
}
func
newFakeKeycloakProxy
(
t
*
testing
.
T
)
*
KeycloakProxy
{
log
.
SetOutput
(
ioutil
.
Discard
)
kc
:=
&
KeycloakProxy
{
config
:
&
Config
{
func
newFakeKeycloakConfig
(
t
*
testing
.
T
)
*
Config
{
return
&
Config
{
DiscoveryURL
:
"127.0.0.1:"
,
ClientID
:
fakeClientID
,
Secret
:
fakeSecret
,
...
...
@@ -95,7 +93,14 @@ func newFakeKeycloakProxy(t *testing.T) *KeycloakProxy {
},
},
CORS
:
&
CORS
{},
},
}
}
func
newFakeKeycloakProxy
(
t
*
testing
.
T
)
*
KeycloakProxy
{
log
.
SetOutput
(
ioutil
.
Discard
)
kc
:=
&
KeycloakProxy
{
config
:
newFakeKeycloakConfig
(
t
),
proxy
:
new
(
fakeReverseProxy
),
}
kc
.
router
=
gin
.
New
()
...
...
@@ -106,21 +111,50 @@ func newFakeKeycloakProxy(t *testing.T) *KeycloakProxy {
return
kc
}
func
TestNewKeycloakProxy
(
t
*
testing
.
T
)
{
proxy
,
err
:=
newKeycloakProxy
(
newFakeKeycloakConfig
(
t
))
assert
.
NoError
(
t
,
err
)
assert
.
NotNil
(
t
,
proxy
)
assert
.
NotNil
(
t
,
proxy
.
config
)
assert
.
NotNil
(
t
,
proxy
.
router
)
assert
.
NotNil
(
t
,
proxy
.
upstreamURL
)
}
func
TestRedirectToAuthorization
(
t
*
testing
.
T
)
{
context
:=
newFakeGinContext
(
"GET"
,
"/admin"
)
proxy
:=
newFakeKeycloakProxy
(
t
)
proxy
.
config
.
SkipTokenVerification
=
false
proxy
.
redirectToAuthorization
(
context
)
if
context
.
Writer
.
Status
()
!=
http
.
StatusTemporaryRedirect
{
t
.
Errorf
(
"we should have been given a temporary redirect"
)
assert
.
Equal
(
t
,
http
.
StatusTemporaryRedirect
,
context
.
Writer
.
Status
())
}
func
TestRedirectToAuthorizationSkipToken
(
t
*
testing
.
T
)
{
context
:=
newFakeGinContext
(
"GET"
,
"/admin"
)
proxy
:=
newFakeKeycloakProxy
(
t
)
proxy
.
config
.
SkipTokenVerification
=
true
proxy
.
redirectToAuthorization
(
context
)
if
context
.
Writer
.
Status
()
!=
http
.
StatusForbidden
{
t
.
Errorf
(
"we should have been given a forbidden code"
)
assert
.
Equal
(
t
,
http
.
StatusForbidden
,
context
.
Writer
.
Status
())
}
func
TestRedirectToAuthorizationUnauthorized
(
t
*
testing
.
T
)
{
context
:=
newFakeGinContext
(
"GET"
,
"/admin"
)
proxy
:=
newFakeKeycloakProxy
(
t
)
proxy
.
config
.
SkipTokenVerification
=
false
proxy
.
config
.
NoRedirects
=
true
proxy
.
redirectToAuthorization
(
context
)
assert
.
Equal
(
t
,
http
.
StatusUnauthorized
,
context
.
Writer
.
Status
())
}
func
TestInitializeReverseProxy
(
t
*
testing
.
T
)
{
proxy
:=
newFakeKeycloakProxy
(
t
)
uri
,
_
:=
url
.
Parse
(
"http://127.0.0.1:8080"
)
reverse
,
err
:=
proxy
.
initializeReverseProxy
(
uri
)
assert
.
NoError
(
t
,
err
)
assert
.
NotNil
(
t
,
reverse
)
}
func
TestRedirectURL
(
t
*
testing
.
T
)
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment