-
Notifications
You must be signed in to change notification settings - Fork 30
Add info() to fetch the authenticated record user #190
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
welpie21
wants to merge
3
commits into
surrealdb:main
Choose a base branch
from
welpie21:feature/info-auth
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,118 @@ | ||
| package com.surrealdb; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertFalse; | ||
| import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import com.surrealdb.pojos.User; | ||
| import com.surrealdb.signin.RecordCredential; | ||
| import com.surrealdb.signin.Token; | ||
|
|
||
| /** | ||
| * Tests for {@link Surreal#info()} and {@link Surreal#info(Class)}: fetching | ||
| * the currently-authenticated record user ({@code SELECT * FROM $auth}). | ||
| */ | ||
| public class InfoTests { | ||
|
|
||
| /** Java 8 compatible: Map.of("email", "a@b.com", "pass", "p") */ | ||
| private static Map<String, String> params(String k1, String v1, String k2, String v2) { | ||
| Map<String, String> m = new HashMap<>(); | ||
| m.put(k1, v1); | ||
| m.put(k2, v2); | ||
| return m; | ||
| } | ||
|
|
||
| /** | ||
| * Defines a `user` table (readable by its own record user) with a record | ||
| * access, then signs up a record user with the given email. | ||
| */ | ||
| private static Token signupUser(Surreal surreal, String email) { | ||
| return signupUser(surreal, email, "WHERE id = $auth.id"); | ||
| } | ||
|
|
||
| /** | ||
| * Defines a `user` table with the given select permission clause and a record | ||
| * access, then signs up a record user with the given email. | ||
| */ | ||
| private static Token signupUser(Surreal surreal, String email, String selectPermission) { | ||
| surreal.query("DEFINE TABLE user SCHEMALESS PERMISSIONS FOR select " + selectPermission); | ||
| surreal.query("DEFINE ACCESS user ON DATABASE TYPE RECORD " | ||
| + "SIGNUP ( CREATE user SET email = $email, pass = crypto::argon2::generate($pass) ) " | ||
| + "SIGNIN ( SELECT * FROM user WHERE email = $email AND crypto::argon2::compare(pass, $pass) ) " | ||
| + "DURATION FOR TOKEN 15m, FOR SESSION 12h"); | ||
| return surreal.signup(new RecordCredential("user", params("email", email, "pass", "p"))); | ||
| } | ||
|
|
||
| @Test | ||
| void info_afterSignup_returnsAuthenticatedRecord() { | ||
| try (Surreal surreal = new Surreal()) { | ||
| surreal.connect("memory").useNs("test_ns").useDb("test_db"); | ||
| Token token = signupUser(surreal, "tobie@example.com"); | ||
| assertNotNull(token.getAccess()); | ||
| // info() returns the record user pointed to by $auth | ||
| Optional<Value> info = surreal.info(); | ||
| assertTrue(info.isPresent()); | ||
| Value value = info.get(); | ||
| assertTrue(value.isObject()); | ||
| assertEquals("tobie@example.com", value.getObject().get("email").getString()); | ||
| // The returned record is the $auth record | ||
| RecordId auth = surreal.query("RETURN $auth").take(0).getRecordId(); | ||
| assertEquals(auth, value.getObject().get("id").getRecordId()); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void info_typed_hydratesPojo() { | ||
| try (Surreal surreal = new Surreal()) { | ||
| surreal.connect("memory").useNs("test_ns").useDb("test_db"); | ||
| signupUser(surreal, "jaime@example.com"); | ||
| Optional<User> user = surreal.info(User.class); | ||
| assertTrue(user.isPresent()); | ||
| assertEquals("jaime@example.com", user.get().email); | ||
| assertNotNull(user.get().id); | ||
| assertEquals("user", user.get().id.getTable()); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void info_unauthenticated_returnsEmpty() { | ||
| try (Surreal surreal = new Surreal()) { | ||
| surreal.connect("memory").useNs("test_ns").useDb("test_db"); | ||
| assertFalse(surreal.info().isPresent()); | ||
| assertFalse(surreal.info(User.class).isPresent()); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void info_recordNotVisibleToUser_returnsEmpty() { | ||
| try (Surreal surreal = new Surreal()) { | ||
| surreal.connect("memory").useNs("test_ns").useDb("test_db"); | ||
| // The table's select permission excludes the record user's own record | ||
| Token token = signupUser(surreal, "denied@example.com", "NONE"); | ||
| assertNotNull(token.getAccess()); | ||
| // The session is authenticated: $auth points to the record user... | ||
| RecordId auth = surreal.query("RETURN $auth").take(0).getRecordId(); | ||
| assertEquals("user", auth.getTable()); | ||
| // ...but the record is not visible to it, so info() is empty | ||
| assertFalse(surreal.info().isPresent()); | ||
| assertFalse(surreal.info(User.class).isPresent()); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void info_afterInvalidate_returnsEmpty() { | ||
| try (Surreal surreal = new Surreal()) { | ||
| surreal.connect("memory").useNs("test_ns").useDb("test_db"); | ||
| signupUser(surreal, "emmanuel@example.com"); | ||
| assertTrue(surreal.info().isPresent()); | ||
| surreal.invalidate(); | ||
| assertFalse(surreal.info().isPresent()); | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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 com.surrealdb.pojos; | ||
|
|
||
| import java.util.Objects; | ||
|
|
||
| import com.surrealdb.RecordId; | ||
|
|
||
| public class User { | ||
|
|
||
| public RecordId id; | ||
| public String email; | ||
|
|
||
| public User() { | ||
| } | ||
|
|
||
| public User(RecordId id, String email) { | ||
| this.id = id; | ||
| this.email = email; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) | ||
| return true; | ||
| if (o == null || getClass() != o.getClass()) | ||
| return false; | ||
| final User user = (User) o; | ||
| return Objects.equals(id, user.id) && Objects.equals(email, user.email); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "id: " + id + ", email: " + email; | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(id, email); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Javadoc says empty is also returned when "the record is not visible to it" (permissions). That's plausible but untested and server-version-dependent — the only permissions case covered is the permissive
WHERE id = $auth.id. Consider softening the wording or adding a test.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a test rather than softening the wording:
info_recordNotVisibleToUser_returnsEmpty(8e375e8) signs up a record user against a table defined withPERMISSIONS FOR select NONE, asserts the session is still authenticated (RETURN $authyields auserrecord id), and then asserts bothinfo()andinfo(Class)return an empty Optional. So the behavior matches the Javadoc on the embedded engine (SurrealDB 3.1.5):SELECT * FROM $authreturns an empty result set when permissions hide the record, whichinfo()maps toOptional.empty(). Kept the Javadoc as-is now that the claim is pinned by a test.