-
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.
Merge branch 'main' into dependabot/go_modules/backend/backend-b42804…
…ba1b
- Loading branch information
Showing
38 changed files
with
1,020 additions
and
158 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
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,52 @@ | ||
package auth | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/GenerateNU/sac/backend/utilities" | ||
"github.com/gofiber/fiber/v2" | ||
"github.com/google/uuid" | ||
) | ||
|
||
type localsKey byte | ||
|
||
const ( | ||
claimsKey localsKey = 0 | ||
userIDKey localsKey = 1 | ||
) | ||
|
||
func CustomClaimsFrom(c *fiber.Ctx) (*CustomClaims, error) { | ||
rawClaims := c.Locals(claimsKey) | ||
if rawClaims == nil { | ||
return nil, utilities.Forbidden() | ||
} | ||
|
||
claims, ok := rawClaims.(*CustomClaims) | ||
if !ok { | ||
return nil, fmt.Errorf("claims are not of type CustomClaims. got: %T", rawClaims) | ||
} | ||
|
||
return claims, nil | ||
} | ||
|
||
func SetClaims(c *fiber.Ctx, claims *CustomClaims) { | ||
c.Locals(claimsKey, claims) | ||
} | ||
|
||
func UserIDFrom(c *fiber.Ctx) (*uuid.UUID, error) { | ||
userID := c.Locals(userIDKey) | ||
if userID == nil { | ||
return nil, utilities.Forbidden() | ||
} | ||
|
||
id, ok := userID.(*uuid.UUID) | ||
if !ok { | ||
return nil, fmt.Errorf("userID is not of type uuid.UUID. got: %T", userID) | ||
} | ||
|
||
return id, nil | ||
} | ||
|
||
func SetUserID(c *fiber.Ctx, id *uuid.UUID) { | ||
c.Locals(userIDKey, id) | ||
} |
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,102 @@ | ||
package config | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
|
||
m "github.com/garrettladley/mattress" | ||
) | ||
|
||
type OAuthSettings struct { | ||
BaseURL string | ||
TokenURL string | ||
ClientID *m.Secret[string] | ||
ClientSecret *m.Secret[string] | ||
Scopes string | ||
RedirectURI string | ||
ResponseType string | ||
ResponseMode string | ||
AccessType string | ||
IncludeGrantedScopes string | ||
Prompt string | ||
} | ||
|
||
type OAuthResources struct { | ||
GoogleOAuthSettings *OAuthSettings | ||
OutlookOAuthSettings *OAuthSettings | ||
} | ||
|
||
/** | ||
**/ | ||
func readGoogleOAuthSettings() (*OAuthSettings, error) { | ||
clientID := os.Getenv("GOOGLE_OAUTH_CLIENT_ID") | ||
if clientID == "" { | ||
return nil, errors.New("GOOGLE_OAUTH_CLIENT_ID is not set") | ||
} | ||
|
||
secretClientID, err := m.NewSecret(clientID) | ||
if err != nil { | ||
return nil, errors.New("failed to create secret from client ID") | ||
} | ||
|
||
clientSecret := os.Getenv("GOOGLE_OAUTH_CLIENT_SECRET") | ||
if clientSecret == "" { | ||
return nil, errors.New("GOOGLE_OAUTH_CLIENT_SECRET is not set") | ||
} | ||
|
||
secretClientSecret, err := m.NewSecret(clientSecret) | ||
if err != nil { | ||
return nil, errors.New("failed to create secret from client secret") | ||
} | ||
|
||
return &OAuthSettings{ | ||
BaseURL: "https://accounts.google.com/o/oauth2/v2", | ||
TokenURL: "https://oauth2.googleapis.com", | ||
ClientID: secretClientID, | ||
ClientSecret: secretClientSecret, | ||
Scopes: "https://www.googleapis.com/auth/calendar.events https://www.googleapis.com/auth/calendar.readonly", | ||
ResponseType: "code", | ||
RedirectURI: "http://localhost:3000", | ||
IncludeGrantedScopes: "true", | ||
AccessType: "offline", | ||
Prompt: "consent", | ||
}, nil | ||
} | ||
|
||
/** | ||
* OUTLOOK | ||
**/ | ||
func readOutlookOAuthSettings() (*OAuthSettings, error) { | ||
clientID := os.Getenv("OUTLOOK_OAUTH_CLIENT_ID") | ||
if clientID == "" { | ||
return nil, errors.New("OUTLOOK_OAUTH_CLIENT_ID is not set") | ||
} | ||
|
||
secretClientID, err := m.NewSecret(clientID) | ||
if err != nil { | ||
return nil, errors.New("failed to create secret from client ID") | ||
} | ||
|
||
clientSecret := os.Getenv("OUTLOOK_OAUTH_CLIENT_SECRET") | ||
if clientSecret == "" { | ||
return nil, errors.New("OUTLOOK_OAUTH_CLIENT_SECRET is not set") | ||
} | ||
|
||
secretClientSecret, err := m.NewSecret(clientSecret) | ||
if err != nil { | ||
return nil, errors.New("failed to create secret from client secret") | ||
} | ||
|
||
return &OAuthSettings{ | ||
BaseURL: "https://login.microsoftonline.com/common/oauth2/v2.0", | ||
TokenURL: "https://login.microsoftonline.com/common/oauth2/v2.0", | ||
ClientID: secretClientID, | ||
ClientSecret: secretClientSecret, | ||
Scopes: "offline_access user.read calendars.readwrite", | ||
ResponseType: "code", | ||
RedirectURI: "http://localhost:3000", | ||
ResponseMode: "query", | ||
Prompt: "consent", | ||
}, 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
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,35 @@ | ||
package models | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/google/uuid" | ||
) | ||
|
||
type OAuthResource string | ||
|
||
const ( | ||
Google OAuthResource = "google" | ||
Outlook OAuthResource = "outlook" | ||
) | ||
|
||
type UserOAuthTokens struct { | ||
UserID uuid.UUID `json:"user_id" validate:"required,uuid4"` | ||
RefreshToken string `json:"refresh_token" validate:"max=255"` | ||
AccessToken string `json:"access_token" validate:"max=255"` | ||
CSRFToken string `json:"csrf_token" validate:"max=255"` | ||
ResourceType OAuthResource `json:"resource_type" validate:"required"` | ||
ExpiresAt time.Time `json:"expires_at" validate:"required"` | ||
} | ||
|
||
type OAuthToken struct { | ||
AccessToken string `json:"access_token" validate:"required"` | ||
ExpiresIn int `json:"expires_in" validate:"required"` | ||
RefreshToken string `json:"refresh_token" validate:"required"` | ||
Scope string `json:"scope" validate:"required"` | ||
TokenType string `json:"token_type" validate:"required"` | ||
} | ||
|
||
func (UserOAuthTokens) TableName() string { | ||
return "user_oauth_tokens" | ||
} |
Oops, something went wrong.