Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
cashier
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Kevin Lyda
cashier
Commits
23e363c2
Commit
23e363c2
authored
6 years ago
by
Kevin Lyda
Browse files
Options
Downloads
Patches
Plain Diff
Fix the gitlab oauth issue.
Now the problem is tha API is wrong. Sigh...
parent
c82e7ee1
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
server/auth/gitlab/gitlab.go
+9
-0
9 additions, 0 deletions
server/auth/gitlab/gitlab.go
server/handlers.go
+5
-0
5 additions, 0 deletions
server/handlers.go
server/server.go
+3
-0
3 additions, 0 deletions
server/server.go
with
17 additions
and
0 deletions
server/auth/gitlab/gitlab.go
+
9
−
0
View file @
23e363c2
...
@@ -2,6 +2,7 @@ package gitlab
...
@@ -2,6 +2,7 @@ package gitlab
import
(
import
(
"errors"
"errors"
"log"
"strconv"
"strconv"
"github.com/nsheridan/cashier/server/config"
"github.com/nsheridan/cashier/server/config"
...
@@ -46,6 +47,7 @@ func New(c *config.Auth) (*Config, error) {
...
@@ -46,6 +47,7 @@ func New(c *config.Auth) (*Config, error) {
return
nil
,
errors
.
New
(
"gitlab_opts if allusers is set, siteurl must be set"
)
return
nil
,
errors
.
New
(
"gitlab_opts if allusers is set, siteurl must be set"
)
}
}
}
}
oauth2
.
RegisterBrokenAuthHeaderProvider
(
siteURL
)
return
&
Config
{
return
&
Config
{
config
:
&
oauth2
.
Config
{
config
:
&
oauth2
.
Config
{
...
@@ -75,18 +77,22 @@ func (c *Config) Name() string {
...
@@ -75,18 +77,22 @@ func (c *Config) Name() string {
// Valid validates the oauth token.
// Valid validates the oauth token.
func
(
c
*
Config
)
Valid
(
token
*
oauth2
.
Token
)
bool
{
func
(
c
*
Config
)
Valid
(
token
*
oauth2
.
Token
)
bool
{
if
!
token
.
Valid
()
{
if
!
token
.
Valid
()
{
log
.
Printf
(
"Auth fail (oauth2 Valid failure)"
)
return
false
return
false
}
}
if
c
.
allusers
{
if
c
.
allusers
{
log
.
Printf
(
"Auth success (allusers)"
)
metrics
.
M
.
AuthValid
.
WithLabelValues
(
"gitlab"
)
.
Inc
()
metrics
.
M
.
AuthValid
.
WithLabelValues
(
"gitlab"
)
.
Inc
()
return
true
return
true
}
}
if
len
(
c
.
whitelist
)
>
0
&&
!
c
.
whitelist
[
c
.
Username
(
token
)]
{
if
len
(
c
.
whitelist
)
>
0
&&
!
c
.
whitelist
[
c
.
Username
(
token
)]
{
log
.
Printf
(
"Auth fail (not in whitelist)"
)
return
false
return
false
}
}
if
c
.
group
==
""
{
if
c
.
group
==
""
{
// There's no group and token is valid. Can only reach
// There's no group and token is valid. Can only reach
// here if user whitelist is set and user is in whitelist.
// here if user whitelist is set and user is in whitelist.
log
.
Printf
(
"Auth success (no groups specified in server config)"
)
metrics
.
M
.
AuthValid
.
WithLabelValues
(
"gitlab"
)
.
Inc
()
metrics
.
M
.
AuthValid
.
WithLabelValues
(
"gitlab"
)
.
Inc
()
return
true
return
true
}
}
...
@@ -94,14 +100,17 @@ func (c *Config) Valid(token *oauth2.Token) bool {
...
@@ -94,14 +100,17 @@ func (c *Config) Valid(token *oauth2.Token) bool {
client
.
SetBaseURL
(
c
.
baseurl
)
client
.
SetBaseURL
(
c
.
baseurl
)
groups
,
_
,
err
:=
client
.
Groups
.
SearchGroup
(
c
.
group
)
groups
,
_
,
err
:=
client
.
Groups
.
SearchGroup
(
c
.
group
)
if
err
!=
nil
{
if
err
!=
nil
{
log
.
Printf
(
"Auth failure (error fetching groups: %s)"
,
err
)
return
false
return
false
}
}
for
_
,
g
:=
range
groups
{
for
_
,
g
:=
range
groups
{
if
g
.
Path
==
c
.
group
{
if
g
.
Path
==
c
.
group
{
metrics
.
M
.
AuthValid
.
WithLabelValues
(
"gitlab"
)
.
Inc
()
metrics
.
M
.
AuthValid
.
WithLabelValues
(
"gitlab"
)
.
Inc
()
log
.
Printf
(
"Auth success (in allowed group)"
)
return
true
return
true
}
}
}
}
log
.
Printf
(
"Auth failure (not in allowed groups)"
)
return
false
return
false
}
}
...
...
This diff is collapsed.
Click to expand it.
server/handlers.go
+
5
−
0
View file @
23e363c2
...
@@ -88,6 +88,7 @@ func (a *app) auth(w http.ResponseWriter, r *http.Request) {
...
@@ -88,6 +88,7 @@ func (a *app) auth(w http.ResponseWriter, r *http.Request) {
case
"/auth/callback"
:
case
"/auth/callback"
:
state
:=
a
.
getSessionVariable
(
r
,
"state"
)
state
:=
a
.
getSessionVariable
(
r
,
"state"
)
if
r
.
FormValue
(
"state"
)
!=
state
{
if
r
.
FormValue
(
"state"
)
!=
state
{
log
.
Printf
(
"Not authorized on /auth/callback"
)
w
.
WriteHeader
(
http
.
StatusUnauthorized
)
w
.
WriteHeader
(
http
.
StatusUnauthorized
)
w
.
Write
([]
byte
(
http
.
StatusText
(
http
.
StatusUnauthorized
)))
w
.
Write
([]
byte
(
http
.
StatusText
(
http
.
StatusUnauthorized
)))
break
break
...
@@ -99,11 +100,13 @@ func (a *app) auth(w http.ResponseWriter, r *http.Request) {
...
@@ -99,11 +100,13 @@ func (a *app) auth(w http.ResponseWriter, r *http.Request) {
code
:=
r
.
FormValue
(
"code"
)
code
:=
r
.
FormValue
(
"code"
)
token
,
err
:=
a
.
authprovider
.
Exchange
(
code
)
token
,
err
:=
a
.
authprovider
.
Exchange
(
code
)
if
err
!=
nil
{
if
err
!=
nil
{
log
.
Printf
(
"Error on /auth/callback: %v"
,
err
)
w
.
WriteHeader
(
http
.
StatusInternalServerError
)
w
.
WriteHeader
(
http
.
StatusInternalServerError
)
w
.
Write
([]
byte
(
http
.
StatusText
(
http
.
StatusInternalServerError
)))
w
.
Write
([]
byte
(
http
.
StatusText
(
http
.
StatusInternalServerError
)))
w
.
Write
([]
byte
(
err
.
Error
()))
w
.
Write
([]
byte
(
err
.
Error
()))
break
break
}
}
log
.
Printf
(
"Token found on /auth/callback, redirecting to %s"
,
originURL
)
a
.
setAuthToken
(
w
,
r
,
token
)
a
.
setAuthToken
(
w
,
r
,
token
)
http
.
Redirect
(
w
,
r
,
originURL
,
http
.
StatusFound
)
http
.
Redirect
(
w
,
r
,
originURL
,
http
.
StatusFound
)
default
:
default
:
...
@@ -112,7 +115,9 @@ func (a *app) auth(w http.ResponseWriter, r *http.Request) {
...
@@ -112,7 +115,9 @@ func (a *app) auth(w http.ResponseWriter, r *http.Request) {
}
}
func
(
a
*
app
)
index
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
func
(
a
*
app
)
index
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
log
.
Printf
(
"Entering index handler."
)
tok
:=
a
.
getAuthToken
(
r
)
tok
:=
a
.
getAuthToken
(
r
)
log
.
Printf
(
"Token found: %v
\n
"
,
tok
)
page
:=
struct
{
page
:=
struct
{
Token
string
Token
string
}{
tok
.
AccessToken
}
}{
tok
.
AccessToken
}
...
...
This diff is collapsed.
Click to expand it.
server/server.go
+
3
−
0
View file @
23e363c2
...
@@ -251,8 +251,11 @@ func (a *app) setSessionVariable(w http.ResponseWriter, r *http.Request, key, va
...
@@ -251,8 +251,11 @@ func (a *app) setSessionVariable(w http.ResponseWriter, r *http.Request, key, va
func
(
a
*
app
)
authed
(
next
http
.
Handler
)
http
.
Handler
{
func
(
a
*
app
)
authed
(
next
http
.
Handler
)
http
.
Handler
{
return
http
.
HandlerFunc
(
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
return
http
.
HandlerFunc
(
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
log
.
Printf
(
"Checking auth for %s."
,
r
.
URL
.
EscapedPath
())
t
:=
a
.
getAuthToken
(
r
)
t
:=
a
.
getAuthToken
(
r
)
log
.
Printf
(
"Token is: %v."
,
t
)
if
!
t
.
Valid
()
||
!
a
.
authprovider
.
Valid
(
t
)
{
if
!
t
.
Valid
()
||
!
a
.
authprovider
.
Valid
(
t
)
{
log
.
Printf
(
"Invalid token t.Valid() = %s."
,
t
.
Valid
())
a
.
setSessionVariable
(
w
,
r
,
"origin_url"
,
r
.
URL
.
EscapedPath
())
a
.
setSessionVariable
(
w
,
r
,
"origin_url"
,
r
.
URL
.
EscapedPath
())
http
.
Redirect
(
w
,
r
,
"/auth/login"
,
http
.
StatusSeeOther
)
http
.
Redirect
(
w
,
r
,
"/auth/login"
,
http
.
StatusSeeOther
)
return
return
...
...
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