-
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.
* Use built-in fetch function to get JWKS * Move user validation to separate file Also renamed the corresponding test file. * Move permissions folder outside auth
- Loading branch information
1 parent
612b4d7
commit faa326f
Showing
9 changed files
with
51 additions
and
61 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
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,47 @@ | ||
package auth | ||
|
||
import ( | ||
"errors" | ||
"net/url" | ||
|
||
"github.com/source-academy/stories-backend/internal/database" | ||
userenums "github.com/source-academy/stories-backend/internal/enums/users" | ||
"github.com/source-academy/stories-backend/model" | ||
"gorm.io/gorm" | ||
) | ||
|
||
func validateAndGetUser(queryString string, db *gorm.DB) (*model.User, error) { | ||
// Validate valid query string | ||
userData, err := url.ParseQuery(queryString) | ||
if err != nil { | ||
return nil, errors.New(invalidTokenSubjectMessage) | ||
} | ||
|
||
// Validate required fields | ||
requiredFields := []string{usernameKey, loginProviderKey} | ||
for _, field := range requiredFields { | ||
if !userData.Has(field) { | ||
return nil, errors.New(invalidTokenSubjectMessage) | ||
} | ||
} | ||
|
||
// Validate login provider | ||
provider, ok := userenums.LoginProviderFromString(userData.Get(loginProviderKey)) | ||
if !ok { | ||
// Invalid/unsupported login provider | ||
return nil, errors.New(invalidTokenSubjectMessage) | ||
} | ||
|
||
// Validate user | ||
user := model.User{ | ||
Username: userData.Get(usernameKey), | ||
LoginProvider: provider, | ||
} | ||
var dbUser model.User | ||
err = db.Where(&user).First(&dbUser).Error | ||
if err != nil { | ||
return nil, database.HandleDBError(err, "user") | ||
} | ||
|
||
return &dbUser, nil | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.