-
Notifications
You must be signed in to change notification settings - Fork 70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a method to authenticate without a config file #53
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ import ( | |
"crypto/rand" | ||
"encoding/base64" | ||
"encoding/json" | ||
"fmt" | ||
//"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
|
||
|
@@ -44,15 +44,18 @@ var conf *oauth2.Config | |
var state string | ||
var store sessions.CookieStore | ||
|
||
var loginURL string | ||
|
||
func randToken() string { | ||
b := make([]byte, 32) | ||
rand.Read(b) | ||
return base64.StdEncoding.EncodeToString(b) | ||
} | ||
|
||
// Setup the authorization path | ||
func Setup(redirectURL, credFile string, scopes []string, secret []byte) { | ||
func Setup(redirectURL, cLoginURL string, credFile string, scopes []string, secret []byte) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wouldn't modify this function signature. Doing so will break existing functionality and require a major version increment. Could we maybe use a mutator/setter to set the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or something like |
||
store = sessions.NewCookieStore(secret) | ||
loginURL = cLoginURL | ||
var c Credentials | ||
file, err := ioutil.ReadFile(credFile) | ||
if err != nil { | ||
|
@@ -69,6 +72,20 @@ func Setup(redirectURL, credFile string, scopes []string, secret []byte) { | |
} | ||
} | ||
|
||
// Setup the authorization path without a config file | ||
func SetupFromString(redirectURL, cLoginURL string, clientID string, clientSecret string, scopes []string, secret []byte) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function should be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are you suggesting @calston modify the existing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe |
||
store = sessions.NewCookieStore(secret) | ||
loginURL = cLoginURL | ||
|
||
conf = &oauth2.Config{ | ||
ClientID: clientID, | ||
ClientSecret: clientSecret, | ||
RedirectURL: redirectURL, | ||
Scopes: scopes, | ||
Endpoint: google.Endpoint, | ||
} | ||
} | ||
|
||
func Session(name string) gin.HandlerFunc { | ||
return sessions.Sessions(name, store) | ||
} | ||
|
@@ -102,7 +119,8 @@ func Auth() gin.HandlerFunc { | |
session := sessions.Default(ctx) | ||
retrievedState := session.Get("state") | ||
if retrievedState != ctx.Query("state") { | ||
ctx.AbortWithError(http.StatusUnauthorized, fmt.Errorf("Invalid session state: %s", retrievedState)) | ||
ctx.Redirect(302, loginURL) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. redirect makes sense, please just drop commented lines :) |
||
//ctx.AbortWithError(http.StatusUnauthorized, fmt.Errorf("Invalid session state: %s", retrievedState)) | ||
return | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
delete line please