Skip to content

How can i Add authentication for Jsonapi #547

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

Closed
Kum4r4n opened this issue Aug 26, 2019 · 17 comments
Closed

How can i Add authentication for Jsonapi #547

Kum4r4n opened this issue Aug 26, 2019 · 17 comments

Comments

@Kum4r4n
Copy link

Kum4r4n commented Aug 26, 2019

Description

I want to add authentication for JSON API,
our project had a relationship with multiple tables
example =>
there are two entity
1> student
2> Teacher

the student Entity has a many-to-many relationship with the teacher Entity
and vice-versa

Student only can view student details But in this case we use =>
student?include=teacher can get the Teachers details also how to prevent include function with
role-based from JWT token

...

Environment

  • JsonApiDotNetCore Version:
  • Other Relevant Package Versions:
  • Microsoft Identity
@wisepotato
Copy link
Contributor

Hi! We support all available authentication schemes.

But do you need authentication (who are you?) compared to authorization(What are you allowed to do) ?

If you mean the latter, we will be integrating that into v4, which is available if you use the alphas. It is also available if you checkout master.

If you want authorization in v3, it is not possible atm.

@wayne-o
Copy link
Contributor

wayne-o commented Aug 27, 2019 via email

@Kum4r4n
Copy link
Author

Kum4r4n commented Aug 27, 2019

@wisepotato
How can I get this alpha ,
Can you give me the Link please

@Kum4r4n
Copy link
Author

Kum4r4n commented Aug 27, 2019

@wisepotato can you share any documentation for that

@wisepotato
Copy link
Contributor

wisepotato commented Aug 27, 2019

Hi, all documentation on v4 is being worked on, together with a demo. My colleague will be finishing up a big bug before we release v4. @maurei could you share your insights on this?

Also, if you look at the tests in master you will see how we use it. We support hooks in which you can hook into ?include'd items.

https://github.com/json-api-dotnet/JsonApiDotNetCore/blob/d6d4a6147de8d915b2491e637d8396639ab933db/docs/usage/resources/hooks.md

Is some preliminary documentation

@wisepotato
Copy link
Contributor

note: this documentation is not final but it will show you how we intend to use it. So use at your own risk atm.

@wayne-o
Copy link
Contributor

wayne-o commented Aug 27, 2019 via email

@wisepotato
Copy link
Contributor

Going to close this as its slated for V4, you can use that to develop further but it wont be finalized until release.

@maurei
Copy link
Member

maurei commented Aug 28, 2019

Docs are very outdated. I need to update these. Also, I was planning on creating a boiler plate authorization project (using hooks) and writing a blog post about it.

You can still the linked docs to get general idea of how they work: conceptually nothing has changed. But for the most recent syntax: best would be to see a few of the integration tests (see the JsonApiDotNetCoreExample project) and the associated methods in the interface of ResourceDefinition in which the hooks are to be implemented

If you have any questions, hit me up in our gitter channel.

@Kum4r4n
Copy link
Author

Kum4r4n commented Aug 30, 2019

can I Authenticate include from IJsonApiContext within the controller?

@maurei
Copy link
Member

maurei commented Aug 30, 2019

I dont understand your question

@Kum4r4n
Copy link
Author

Kum4r4n commented Aug 30, 2019

@maurei
Now I am using JsonApi v3, I want to enable authentication for my project,
in this case, I am using Microsoft identity for that
but problem is I add the [Authorize(Role=" student")] in controller the student able to get the teacher data by include relationship within URLs, How can i prevent from this,

@maurei
Copy link
Member

maurei commented Aug 30, 2019

For this you would need to use the Resource Hooks which are introduced in v4.

The implementation would look like something similar to this example from the docs

Adjusted to your case, it would look something like this. It assumes the existence of a Teacher model. For any request that triggers the OnReturn hook (see trigger overview) a 403 will be thrown if the Teacher resource is involved and the authenticated user has role student.

    public class TeacherResource : ResourceDefinition<Teacher>
    {
        private readonly _IAuthorizationHelper _auth;
        public PersonResource(IAuthorizationHelper auth)
        {
            // IAuthorizationHelper is a helper service that handles all authorization related logic
            // You need to add the desired helper methods yourself in this class.
            _auth = auth;
        }

        public override IEnumerable<Teacher> OnReturn(HashSet<Teacher> entities, ResourcePipeline pipeline)
        {
            if (_auth.HasRole("student")) // you'll have to implement this yourself
            {
                throw new JsonApiException(403, "Forbidden to view this teacher", new UnauthorizedAccessException());
            }
            return entities;
        }
    }

Let me know if this is helpful

@Kum4r4n
Copy link
Author

Kum4r4n commented Aug 30, 2019

I followed this step for intergret JsonApi
https://json-api-dotnet.github.io/#/step-by-step

can you explain where I initialize and how to call from the controller

@Kum4r4n
Copy link
Author

Kum4r4n commented Aug 30, 2019

Can you guide me the follow of this, with JSON API v3 by one example of code

@Kum4r4n
Copy link
Author

Kum4r4n commented Aug 30, 2019

_IAuthorizationHelper _auth, what is included within this class

@maurei
Copy link
Member

maurei commented Aug 30, 2019

Can you guide me the follow of this, with JSON API v3 by one example of code

Authorization requires business logic in the service layer of JADNC, and there is no out of the box support for doing this easily in v3. In v4 Resource Hooks are introduced which allow this. An extensive tutorial on how to implement this in v4 will be released in the near future

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

4 participants