-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Albin Vass
committed
May 26, 2024
1 parent
2020ecb
commit 49fe814
Showing
10 changed files
with
321 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,5 @@ devenv.local.nix | |
|
||
# pre-commit | ||
.pre-commit-config.yaml | ||
|
||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// platform/authenticator/auth.go | ||
|
||
package authenticator | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"os" | ||
|
||
"github.com/coreos/go-oidc/v3/oidc" | ||
"golang.org/x/oauth2" | ||
) | ||
|
||
// Authenticator is used to authenticate our users. | ||
type Authenticator struct { | ||
*oidc.Provider | ||
oauth2.Config | ||
} | ||
|
||
// New instantiates the *Authenticator. | ||
func New() (*Authenticator, error) { | ||
provider, err := oidc.NewProvider( | ||
context.Background(), | ||
"https://"+os.Getenv("AUTH0_DOMAIN")+"/", | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
conf := oauth2.Config{ | ||
ClientID: os.Getenv("AUTH0_CLIENT_ID"), | ||
ClientSecret: os.Getenv("AUTH0_CLIENT_SECRET"), | ||
RedirectURL: os.Getenv("AUTH0_CALLBACK_URL"), | ||
Endpoint: provider.Endpoint(), | ||
Scopes: []string{oidc.ScopeOpenID, "profile"}, | ||
} | ||
|
||
return &Authenticator{ | ||
Provider: provider, | ||
Config: conf, | ||
}, nil | ||
} | ||
|
||
// VerifyIDToken verifies that an *oauth2.Token is a valid *oidc.IDToken. | ||
func (a *Authenticator) VerifyIDToken(ctx context.Context, token *oauth2.Token) (*oidc.IDToken, error) { | ||
rawIDToken, ok := token.Extra("id_token").(string) | ||
if !ok { | ||
return nil, errors.New("no id_token field in oauth2 token") | ||
} | ||
|
||
oidcConfig := &oidc.Config{ | ||
ClientID: a.ClientID, | ||
} | ||
|
||
return a.Verifier(oidcConfig).Verify(ctx, rawIDToken) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// web/app/callback/callback.go | ||
|
||
package callback | ||
|
||
import ( | ||
"context" | ||
"encoding/base64" | ||
"encoding/json" | ||
"net/http" | ||
|
||
"github.com/albinvass/splitfree/splitfree-backend/authenticator" | ||
"github.com/danielgtaylor/huma/v2" | ||
"github.com/google/uuid" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
type CallbackInput struct { | ||
Session http.Cookie `cookie:"session"` | ||
State uuid.UUID `query:"state"` | ||
Code string `query:"code"` | ||
} | ||
|
||
type CallbackOutput struct { | ||
Status int | ||
Cookies []http.Cookie `header:"Set-Cookie"` | ||
Url string `header:"Location"` | ||
} | ||
|
||
// Handler for our callback. | ||
func Handler(auth *authenticator.Authenticator) func(context.Context, *CallbackInput) (*CallbackOutput, error) { | ||
return func(ctx context.Context, callbackInput *CallbackInput) (*CallbackOutput, error) { | ||
state, err := uuid.Parse(callbackInput.Session.Value) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if callbackInput.State != state { | ||
return nil, huma.Error400BadRequest("Invalid state parameter.") | ||
} | ||
|
||
// Exchange an authorization code for a token. | ||
token, err := auth.Exchange(ctx, callbackInput.Code) | ||
if err != nil { | ||
return nil, huma.Error401Unauthorized("Failed to exchange an authorization code for a token.") | ||
} | ||
|
||
idToken, err := auth.VerifyIDToken(ctx, token) | ||
if err != nil { | ||
return nil, huma.Error500InternalServerError("Failed to verify ID Token.") | ||
} | ||
|
||
var profile map[string]interface{} | ||
if err := idToken.Claims(&profile); err != nil { | ||
return nil, huma.Error500InternalServerError("", err) | ||
} | ||
|
||
accessTokenCookie := http.Cookie{ | ||
Name: "access_token", | ||
Value: token.AccessToken, | ||
} | ||
|
||
profileJson, err := json.Marshal(profile) | ||
if err != nil { | ||
return nil, huma.Error500InternalServerError("", err) | ||
} | ||
|
||
profileJsonBase64 := base64.StdEncoding.EncodeToString(profileJson) | ||
|
||
log.Infof("storing cookie: %s", profileJsonBase64) | ||
profileCookie := http.Cookie{ | ||
Name: "profile", | ||
Value: profileJsonBase64, | ||
} | ||
|
||
return &CallbackOutput{ | ||
Status: http.StatusTemporaryRedirect, | ||
Cookies: []http.Cookie{ | ||
accessTokenCookie, | ||
profileCookie, | ||
}, | ||
Url: "/user", | ||
}, nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package login | ||
|
||
import ( | ||
"context" | ||
"github.com/albinvass/splitfree/splitfree-backend/authenticator" | ||
"github.com/google/uuid" | ||
"golang.org/x/oauth2" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
type LoginOutput struct { | ||
Status int | ||
Url string `header:"Location"` | ||
Cookie http.Cookie `header:"Set-Cookie"` | ||
} | ||
|
||
func Handler(auth *authenticator.Authenticator) func(context.Context, *struct{}) (*LoginOutput, error) { | ||
return func(ctx context.Context, _ *struct{}) (*LoginOutput, error) { | ||
state := uuid.New() | ||
|
||
url := auth.AuthCodeURL(state.String(), oauth2.AccessTypeOnline) | ||
|
||
cookie := http.Cookie{ | ||
Name: "session", | ||
Value: state.String(), | ||
Path: "/", | ||
MaxAge: int(time.Hour.Seconds()), | ||
Secure: false, | ||
HttpOnly: true, | ||
} | ||
|
||
return &LoginOutput{ | ||
Status: http.StatusTemporaryRedirect, | ||
Url: url, | ||
Cookie: cookie, | ||
}, nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// web/app/logout/logout.go | ||
|
||
package logout | ||
|
||
import ( | ||
"context" | ||
"github.com/danielgtaylor/huma/v2" | ||
"net/http" | ||
"net/url" | ||
"os" | ||
) | ||
|
||
type LogoutInput struct { | ||
} | ||
|
||
type LogoutOutput struct { | ||
Status int | ||
Url string `header:"Location"` | ||
} | ||
|
||
// Handler for our logout. | ||
func Handler(ctx context.Context, loginInput *LogoutInput) (*LogoutOutput, error) { | ||
logoutUrl, err := url.Parse("https://" + os.Getenv("AUTH0_DOMAIN") + "/v2/logout") | ||
if err != nil { | ||
return nil, huma.Error500InternalServerError("", err) | ||
} | ||
|
||
canonicalUrl := os.Getenv("SPLITFREE_CANONICAL_URL") | ||
|
||
returnTo, err := url.Parse(canonicalUrl) | ||
if err != nil { | ||
return nil, huma.Error500InternalServerError("", err) | ||
} | ||
|
||
parameters := url.Values{} | ||
parameters.Add("returnTo", returnTo.String()) | ||
parameters.Add("client_id", os.Getenv("AUTH0_CLIENT_ID")) | ||
logoutUrl.RawQuery = parameters.Encode() | ||
|
||
return &LogoutOutput{ | ||
Status: http.StatusTemporaryRedirect, | ||
Url: logoutUrl.String(), | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.