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
bd1e6a57
Commit
bd1e6a57
authored
9 years ago
by
Niall Sheridan
Browse files
Options
Downloads
Patches
Plain Diff
Add github oauth provider.
parent
5a57870d
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
server/auth/github/github.go
+95
-0
95 additions, 0 deletions
server/auth/github/github.go
server/auth/github/github_test.go
+49
-0
49 additions, 0 deletions
server/auth/github/github_test.go
server/main.go
+22
-2
22 additions, 2 deletions
server/main.go
with
166 additions
and
2 deletions
server/auth/github/github.go
0 → 100644
+
95
−
0
View file @
bd1e6a57
package
github
import
(
"net/http"
"github.com/nsheridan/cashier/server/auth"
"github.com/nsheridan/cashier/server/config"
githubapi
"github.com/google/go-github/github"
"golang.org/x/oauth2"
"golang.org/x/oauth2/github"
)
const
(
// revokeURL = "https://accounts.google.com/o/oauth2/revoke?token=%s"
name
=
"github"
)
// Config is an implementation of `auth.Provider` for authenticating using a
// Github account.
type
Config
struct
{
config
*
oauth2
.
Config
organization
string
}
// New creates a new Github provider from a configuration.
func
New
(
c
*
config
.
Auth
)
auth
.
Provider
{
return
&
Config
{
config
:
&
oauth2
.
Config
{
ClientID
:
c
.
OauthClientID
,
ClientSecret
:
c
.
OauthClientSecret
,
RedirectURL
:
c
.
OauthCallbackURL
,
Endpoint
:
github
.
Endpoint
,
Scopes
:
[]
string
{
string
(
githubapi
.
ScopeUser
),
string
(
githubapi
.
ScopeReadOrg
),
},
},
organization
:
c
.
ProviderOpts
[
"organization"
],
}
}
// A new oauth2 http client.
func
(
c
*
Config
)
newClient
(
token
*
oauth2
.
Token
)
*
http
.
Client
{
return
c
.
config
.
Client
(
oauth2
.
NoContext
,
token
)
}
// Name returns the name of the provider.
func
(
c
*
Config
)
Name
()
string
{
return
name
}
// Valid validates the oauth token.
func
(
c
*
Config
)
Valid
(
token
*
oauth2
.
Token
)
bool
{
if
!
token
.
Valid
()
{
return
false
}
if
c
.
organization
==
""
{
return
true
}
client
:=
githubapi
.
NewClient
(
c
.
newClient
(
token
))
member
,
_
,
err
:=
client
.
Organizations
.
IsMember
(
c
.
organization
,
c
.
Username
(
token
))
if
err
!=
nil
{
return
false
}
return
member
}
// Revoke disables the access token.
func
(
c
*
Config
)
Revoke
(
token
*
oauth2
.
Token
)
error
{
return
nil
}
// StartSession retrieves an authentication endpoint from Github.
func
(
c
*
Config
)
StartSession
(
state
string
)
*
auth
.
Session
{
return
&
auth
.
Session
{
AuthURL
:
c
.
config
.
AuthCodeURL
(
state
),
State
:
state
,
}
}
// Exchange authorizes the session and returns an access token.
func
(
c
*
Config
)
Exchange
(
code
string
)
(
*
oauth2
.
Token
,
error
)
{
return
c
.
config
.
Exchange
(
oauth2
.
NoContext
,
code
)
}
// Username retrieves the username portion of the user's email address.
func
(
c
*
Config
)
Username
(
token
*
oauth2
.
Token
)
string
{
client
:=
githubapi
.
NewClient
(
c
.
newClient
(
token
))
u
,
_
,
err
:=
client
.
Users
.
Get
(
""
)
if
err
!=
nil
{
return
""
}
return
*
u
.
Login
}
This diff is collapsed.
Click to expand it.
server/auth/github/github_test.go
0 → 100644
+
49
−
0
View file @
bd1e6a57
package
github
import
(
"fmt"
"testing"
"github.com/nsheridan/cashier/server/auth"
"github.com/nsheridan/cashier/server/config"
"github.com/stretchr/testify/assert"
)
var
(
oauthClientID
=
"id"
oauthClientSecret
=
"secret"
oauthCallbackURL
=
"url"
organization
=
"exampleorg"
)
func
TestNew
(
t
*
testing
.
T
)
{
a
:=
assert
.
New
(
t
)
p
:=
newGithub
()
g
:=
p
.
(
*
Config
)
a
.
Equal
(
g
.
config
.
ClientID
,
oauthClientID
)
a
.
Equal
(
g
.
config
.
ClientSecret
,
oauthClientSecret
)
a
.
Equal
(
g
.
config
.
RedirectURL
,
oauthCallbackURL
)
a
.
Equal
(
g
.
organization
,
organization
)
}
func
TestStartSession
(
t
*
testing
.
T
)
{
a
:=
assert
.
New
(
t
)
p
:=
newGithub
()
s
:=
p
.
StartSession
(
"test_state"
)
a
.
Equal
(
s
.
State
,
"test_state"
)
a
.
Contains
(
s
.
AuthURL
,
"github.com/login/oauth/authorize"
)
a
.
Contains
(
s
.
AuthURL
,
"state=test_state"
)
a
.
Contains
(
s
.
AuthURL
,
fmt
.
Sprintf
(
"client_id=%s"
,
oauthClientID
))
}
func
newGithub
()
auth
.
Provider
{
c
:=
&
config
.
Auth
{
OauthClientID
:
oauthClientID
,
OauthClientSecret
:
oauthClientSecret
,
OauthCallbackURL
:
oauthCallbackURL
,
ProviderOpts
:
map
[
string
]
string
{
"organization"
:
organization
},
}
return
New
(
c
)
}
This diff is collapsed.
Click to expand it.
server/main.go
+
22
−
2
View file @
bd1e6a57
...
...
@@ -21,6 +21,7 @@ import (
"github.com/gorilla/sessions"
"github.com/nsheridan/cashier/lib"
"github.com/nsheridan/cashier/server/auth"
"github.com/nsheridan/cashier/server/auth/github"
"github.com/nsheridan/cashier/server/auth/google"
"github.com/nsheridan/cashier/server/config"
"github.com/nsheridan/cashier/server/signer"
...
...
@@ -51,7 +52,7 @@ func (a *appContext) getAuthCookie(r *http.Request) *oauth2.Token {
if
err
:=
json
.
Unmarshal
(
t
.
([]
byte
),
&
tok
);
err
!=
nil
{
return
nil
}
if
!
a
.
authprovider
.
Valid
(
&
tok
)
{
if
!
tok
.
Valid
()
{
return
nil
}
return
&
tok
...
...
@@ -136,6 +137,12 @@ func callbackHandler(a *appContext, w http.ResponseWriter, r *http.Request) (int
if
err
:=
a
.
authsession
.
Authorize
(
a
.
authprovider
,
code
);
err
!=
nil
{
return
http
.
StatusInternalServerError
,
err
}
// Github tokens don't have an expiry. Set one so that the session expires
// after a period.
if
a
.
authsession
.
Token
.
Expiry
.
Unix
()
<=
0
{
a
.
authsession
.
Token
.
Expiry
=
time
.
Now
()
.
Add
(
1
*
time
.
Hour
)
}
fmt
.
Println
(
a
.
authsession
.
Token
)
a
.
setAuthCookie
(
w
,
r
,
a
.
authsession
.
Token
)
http
.
Redirect
(
w
,
r
,
"/"
,
http
.
StatusFound
)
return
http
.
StatusFound
,
nil
...
...
@@ -148,6 +155,9 @@ func rootHandler(a *appContext, w http.ResponseWriter, r *http.Request) (int, er
http
.
Redirect
(
w
,
r
,
"/auth/login"
,
http
.
StatusSeeOther
)
return
http
.
StatusSeeOther
,
nil
}
if
!
a
.
authprovider
.
Valid
(
tok
)
{
return
http
.
StatusUnauthorized
,
errors
.
New
(
http
.
StatusText
(
http
.
StatusUnauthorized
))
}
j
:=
jwt
.
New
(
jwt
.
SigningMethodHS256
)
j
.
Claims
[
"token"
]
=
tok
.
AccessToken
j
.
Claims
[
"exp"
]
=
tok
.
Expiry
.
Unix
()
...
...
@@ -203,7 +213,17 @@ func main() {
if
err
!=
nil
{
log
.
Fatal
(
err
)
}
authprovider
:=
google
.
New
(
&
config
.
Auth
)
var
authprovider
auth
.
Provider
switch
config
.
Auth
.
Provider
{
case
"google"
:
authprovider
=
google
.
New
(
&
config
.
Auth
)
case
"github"
:
authprovider
=
github
.
New
(
&
config
.
Auth
)
default
:
log
.
Fatalln
(
"Unknown provider %s"
,
config
.
Auth
.
Provider
)
}
ctx
:=
&
appContext
{
cookiestore
:
sessions
.
NewCookieStore
([]
byte
(
config
.
Server
.
CookieSecret
)),
authprovider
:
authprovider
,
...
...
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