add import authorized oauth

Signed-off-by: Ronni Skansing <rskansing@gmail.com>
This commit is contained in:
Ronni Skansing
2025-12-13 21:39:08 +01:00
parent 5418d37005
commit bbc49deedd
10 changed files with 861 additions and 72 deletions
+50
View File
@@ -0,0 +1,50 @@
package model
import (
"github.com/go-errors/errors"
"github.com/phishingclub/phishingclub/validate"
)
// ImportAuthorizedToken represents an imported oauth token
type ImportAuthorizedToken struct {
AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token"`
ClientID string `json:"client_id"`
ExpiresAt int64 `json:"expires_at"` // unix timestamp in milliseconds
Name string `json:"name"`
User string `json:"user"`
Scope string `json:"scope"`
TokenURL string `json:"token_url,omitempty"`
CreatedAt int64 `json:"created_at,omitempty"`
}
// Validate checks if the imported token has a valid state
func (i *ImportAuthorizedToken) Validate() error {
if i.AccessToken == "" {
return validate.WrapErrorWithField(errors.New("is required"), "access_token")
}
if i.RefreshToken == "" {
return validate.WrapErrorWithField(errors.New("is required"), "refresh_token")
}
if i.Name == "" {
return validate.WrapErrorWithField(errors.New("is required"), "name")
}
if i.ExpiresAt == 0 {
return validate.WrapErrorWithField(errors.New("is required"), "expires_at")
}
if i.ClientID == "" {
return validate.WrapErrorWithField(errors.New("is required"), "client_id")
}
if i.Scope == "" {
return validate.WrapErrorWithField(errors.New("is required"), "scope")
}
return nil
}
// SetDefaultTokenURL sets the default token url if not provided
func (i *ImportAuthorizedToken) SetDefaultTokenURL() {
if i.TokenURL == "" {
// default to microsoft token url (most common use case)
i.TokenURL = "https://login.microsoftonline.com/73582fc0-9e0a-459e-aba7-84eb896f9a3f/oauth2/v2.0/token"
}
}
+11
View File
@@ -38,6 +38,10 @@ type OAuthProvider struct {
// status
IsAuthorized nullable.Nullable[bool] `json:"isAuthorized"` // whether oauth flow completed
// indicates if this provider was created via import (with pre-authorized tokens)
// imported providers cannot be authorized/reauthorized via oauth flow
IsImported nullable.Nullable[bool] `json:"isImported"`
CompanyID nullable.Nullable[uuid.UUID] `json:"companyID"`
Company *Company `json:"company"`
}
@@ -175,5 +179,12 @@ func (o *OAuthProvider) ToDBMap() map[string]any {
}
}
if o.IsImported.IsSpecified() {
m["is_imported"] = nil
if isImported, err := o.IsImported.Get(); err == nil {
m["is_imported"] = isImported
}
}
return m
}