Skip to content
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

Valid x-typesense-api-key header must be sent #115

Closed
elhe26 opened this issue Feb 1, 2022 · 9 comments
Closed

Valid x-typesense-api-key header must be sent #115

elhe26 opened this issue Feb 1, 2022 · 9 comments
Labels
bug Something isn't working

Comments

@elhe26
Copy link

elhe26 commented Feb 1, 2022

Describe the bug

It seems the Configuration object is not setting the API key. Basic configuration:

class SearchService {
Client? client;
String searchOne = "add-endpoint-here";
String searchTwo = "add-endpoint-here";
String searchThree = "add-endpoint-here";

void initialize(String searchKey) {
    var config = Configuration(
      searchKey,
      nodes: {
        Node(Protocol.https, searchOne, port: 443),
        Node(Protocol.https, searchTwo, port: 443),
        Node(Protocol.https, searchThree, port: 443),
        },
      connectionTimeout: Duration(seconds: 2),
      );
      client = Client(config);
   }

  Future<dynamic> Search(
    String? value,
    String queryBy,
    String collection,
  ) async {
    try {
      var search = await client!.collection(collection).documents.search({
        "q": value ?? "*",
        "query_by": queryBy,
      });
         return search;
        } catch(err){
          return err;
        }
    }
}

Related: Typesense Error 476

Steps to reproduce the behavior:

  1. Initialize client - No issues
  2. Search/retrieve documents - Error

Expected behavior

1 . Return either collections or documents.

Additional context

  • Using a shell to query/retrieve any document doesn't cause any issue.
  • Using Admin-Key, Search-only-Key, and Scoped-Key cause the same errors.
  • Using client SDK:

Error 1

401: {"message": "Forbidden - a valid `x-typesense-api-key` header must be sent."}

Error 2
This error is also a weird behavior from the client SDK. All schema variables are defined with type and facet. If the client is not throwing Error 1, it's giving this other error:

400: {"message": "Field `value` should be a string or a string array."}

"value" variable config (Schema):

...
    {
      "facet": false,
      "index": true,
      "name": "value",
      "optional": false,
      "type": "float"
    },
...

Note:
Setting the "sendApiKeyAsQueryParam" variable won't change the outcome.

@elhe26 elhe26 added the bug Something isn't working label Feb 1, 2022
@elhe26
Copy link
Author

elhe26 commented Feb 1, 2022

Update:

Searching, retrievals, etc... using dart's HTTP library generates no issues.

Admin Key & Scoped Search Keys are working without any inconveniences atm.

@happy-san
Copy link
Collaborator

Hey @elhe26
Can you clarify if you're facing 401 error using the same key with shell and SDK?

Using a shell to query/retrieve any document doesn't cause any issue.
Using Admin-Key, Search-only-Key, and Scoped-Key cause the same errors.

Also, have you used the dart SDK to generate the search key?

@happy-san
Copy link
Collaborator

Regarding Error 2,
I suggest creating a separate issue. Also, it would be helpful if you'd include the response of collections/{collection_name}.

@elhe26
Copy link
Author

elhe26 commented Feb 2, 2022

Hey @elhe26 Can you clarify if you're facing 401 error using the same key with shell and SDK?

Using a shell to query/retrieve any document doesn't cause any issue.
Using Admin-Key, Search-only-Key, and Scoped-Key cause the same errors.

Also, have you used the dart SDK to generate the search key?

I created the key from the server using typesense-js. When using the scoped key from the shell, I don't get any issues. When using it from the client I get that error.

@happy-san
Copy link
Collaborator

I created the key from the server using typesense-js.

I'm assuming you're referring to creating a scoped search key here?

@elhe26
Copy link
Author

elhe26 commented Feb 2, 2022

I created the key from the server using typesense-js.

I'm assuming you're referring to creating a scoped search key here?

Yes. I used the admin key to create a search-only key and then I generated the scoped key.

@happy-san
Copy link
Collaborator

@elhe26 I tried to reproduce the issue with the following code and found nothing.

void main(List<String> arguments) async {
  /// Initial search key generated by the typesense cluster.
  final searchKey = 'SSw1XmjxjjcA2Raq0UhNPgPff7XTLbF7';
  final config = Configuration(
    searchKey,
    nodes: {
      Node(
        Protocol.https,
        'Host',
      ),
    },
  );

  final client = Client(config);

  // Generate scoped search key
  final scopedSearchKey = client.keys.generateScopedSearchKey(
    'SSw1XmjxjjcA2Raq0UhNPgPff7XTLbF7',
    {
      'query_by': 'company_name',
      'filter_by': 'country:=abc && num_employees:>0',
    },
  );

  print(scopedSearchKey);

  final configuration = Configuration(
    scopedSearchKey,
    nodes: {
      Node(
        Protocol.https,
        'Host',
      ),
    },
  );

  final clientWithScopedSearchKey = Client(configuration);
  print(
    await clientWithScopedSearchKey.collection('companies').documents.search(
      {
        'q': '',
      },
    ),
  );
}

I believe you're trying to perform an action that's out of the scope of the key. Using the admin key try retrieve and look for the permissions of the key you're using.

@elhe26
Copy link
Author

elhe26 commented Feb 14, 2022

Thanks @happy-san. I'm following the docs but I'm getting the same issue. I'll start over again and see if that works.

@happy-san
Copy link
Collaborator

For future reference, try to log your exact steps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants