mirror of
https://github.com/steveiliop56/tinyauth.git
synced 2025-11-04 16:15:45 +00:00
feat: add CSRF cookie protection
This commit is contained in:
@@ -2,6 +2,8 @@ package oauth
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
"net/http"
|
||||
|
||||
"golang.org/x/oauth2"
|
||||
@@ -26,9 +28,9 @@ func (oauth *OAuth) Init() {
|
||||
oauth.Verifier = oauth2.GenerateVerifier()
|
||||
}
|
||||
|
||||
func (oauth *OAuth) GetAuthURL() string {
|
||||
func (oauth *OAuth) GetAuthURL(state string) string {
|
||||
// Return the auth url
|
||||
return oauth.Config.AuthCodeURL("state", oauth2.AccessTypeOffline, oauth2.S256ChallengeOption(oauth.Verifier))
|
||||
return oauth.Config.AuthCodeURL(state, oauth2.AccessTypeOffline, oauth2.S256ChallengeOption(oauth.Verifier))
|
||||
}
|
||||
|
||||
func (oauth *OAuth) ExchangeToken(code string) (string, error) {
|
||||
@@ -51,3 +53,16 @@ func (oauth *OAuth) GetClient() *http.Client {
|
||||
// Return the http client with the token set
|
||||
return oauth.Config.Client(oauth.Context, oauth.Token)
|
||||
}
|
||||
|
||||
func (oauth *OAuth) GenerateState() string {
|
||||
// Generate a random state string
|
||||
b := make([]byte, 128)
|
||||
|
||||
// Fill the byte slice with random data
|
||||
rand.Read(b)
|
||||
|
||||
// Encode the byte slice to a base64 string
|
||||
state := base64.URLEncoding.EncodeToString(b)
|
||||
|
||||
return state
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user