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

Error Authorization Required #1

Open
bellgetlinks opened this issue Jun 1, 2018 · 16 comments
Open

Error Authorization Required #1

bellgetlinks opened this issue Jun 1, 2018 · 16 comments
Assignees
Labels
question Further information is requested

Comments

@bellgetlinks
Copy link

Hi I figure out how to upload file to s3 to sub folder and I found this :)

By the way I trying upload file and I add access_token={token} but always get 401 error

Example : POST "/api/consumers/{id}/images/{folder}/files/upload?access_token={token}"

I dont know why :(

@regevbr
Copy link
Owner

regevbr commented Jun 1, 2018 via email

@bellgetlinks
Copy link
Author

it's not working :(

@regevbr regevbr self-assigned this Jun 1, 2018
@regevbr regevbr added bug Something isn't working question Further information is requested and removed bug Something isn't working labels Jun 1, 2018
@regevbr
Copy link
Owner

regevbr commented Jun 1, 2018

I think I know what the issue is.
When dealing with ACLs in nested queries, the ACL is actually being assigned and tested only of the first level model of the nesting. In your case it is the consumers model.
To allow the file upload of a consumer you need to add the following (or similar) to the consumers model json file:

 "acls": [
    {
      "accessType": "EXECUTE",
      "principalType": "ROLE",
      "principalId": "$owner",
      "permission": "ALLOW",
      "property": [
        "__upload__images"
      ]
    }
]

Please notice the property "__upload__images" in which images is the name of the realation you gave between the consumer and the images model in the consumers json file.
If that doesn't work for you, you need to figure out the property name for that action that loopack assigned to it. I think one way of doing it is by looking at the generated angular code (lb-ng) or you can debug the code in the "Nested Queries" seciton in my readme file and see the names it produces.
Please let me know if that helps or not so I can continue to help out

@regevbr
Copy link
Owner

regevbr commented Jun 1, 2018

You can also start by enabling all acl actions on the consumers model and see if that works

@bellgetlinks
Copy link
Author

Thanks for your advice, It looks like the error is gone but I found another problem

Seem like s3 generate new folder by using userId :/

And nothing create in images model after upload file

@regevbr
Copy link
Owner

regevbr commented Jun 1, 2018

Good to hear!
From the docs of the component i based this library on:
Upload one or more files into the specified container. The request body must use multipart/form-data which is the file input type for HTML uses.
Are you sure you are passing the proper data in the post body?
Please aee the following code example in https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/file

@bellgetlinks
Copy link
Author

This is what I test upload file just only one file and my s3 bucket create new folder following userId

ps. I try to remove all acl but noting works always error 401
screen shot 2561-06-02 at 02 56 38

@regevbr
Copy link
Owner

regevbr commented Jun 1, 2018

I will try to set uo a sample project tommrow for you to use

@regevbr
Copy link
Owner

regevbr commented Jun 2, 2018

You need to give in the description the file name e.g. screenshot.png that works for me using postman

@regevbr
Copy link
Owner

regevbr commented Jun 2, 2018

As to the ACL, you need to have in the consumers.json file the following relation

  "relations": {
    "images": {
      "type": "hasOne",
      "model": "Image",
      "foreignKey": "userId"
    }
  }

Combined with the ACL

 "acls": [
    {
      "accessType": "EXECUTE",
      "principalType": "ROLE",
      "principalId": "$owner",
      "permission": "ALLOW",
      "property": [
        "__upload__images"
      ]
    }
]

it should work. You must provide and access token of the consumer for it to work in my case as we gave the permission to the owner of the current consumer instance.

This works for both providing the access token in the header or as a query param...

If this still doesnt work for you please post all relevant configuration files so I can see what went wrong

@bellgetlinks
Copy link
Author

Thanks you for your help but It's not working yet. I will start a new project to test first and will try what is wrong :)

@regevbr
Copy link
Owner

regevbr commented Jun 2, 2018

Sure thing keep me posted and share a link to the test project so i can debug it myself

@bellgetlinks
Copy link
Author

Sorry for late this is my repository for test

https://bitbucket.org/bellsenawat/loopback-test-s3-upload/src/master/

Again still not working, S3 still upload to a new folder by using userId

And another question As you set relation of user to images is "hasOne" and I want to set to "hasMany" it can be ?

@regevbr
Copy link
Owner

regevbr commented Jun 2, 2018 via email

@bellgetlinks
Copy link
Author

So for each user have their folder image in s3 right ?
But I confuse why nothing happened in images model and after i upload images nothing return back may be it can be url or something :/

@regevbr
Copy link
Owner

regevbr commented Jun 2, 2018 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants