abjerner / Skybrud.Social

Skybrud.Social is a framework in .NET for integration with various social services like Twitter, Facebook and Instagram. The framework will handle all the technical parts and API communication so you don't have to.
http://social.skybrud.dk/
MIT License
96 stars 32 forks source link

Is possible to use this lib as OAuth2 authentication for members? #65

Closed biapar closed 7 years ago

biapar commented 7 years ago

Is possible to use this lib as OAuth2 authentication for members?

abjerner commented 7 years ago

What kind of members are we talking about?

EmilMoe commented 7 years ago

Community members..?

biapar commented 7 years ago

No. Umbraco member ( sorry ).

biapar commented 7 years ago

I wish to authenticate mobile user ( member in Umbraco ) with their social login.

abjerner commented 7 years ago

You still haven't provided that much information about your use case, but yes, it is possible to use this library for authentication for Umbraco members. However since this library doesn't target Umbraco directly, you'll have to implement some the authentication and membership logic your self.

It's also worth mentioning that this library doesn't just support any OAuth 2.0 service, but only the ones currently supported - eg. Google, Facebook and Instagram. You haven't mentioned a specific service, so I'm using Google as an example.

In standard ASP.NET MVC, you can setup an authentication page for Google accounts as explained on the following page (there is a full example at the bottom): http://social.skybrud.dk/google/authentication/

The example will let you login, and show some brief information about your Google account/profile, but not really much more. The information will be written out something like this:

<div class="alert alert-info">
    <strong>ID:</strong> @user.Id<br />
    <strong>Name:</strong> @user.Name<br />
    <strong>Email:</strong> @user.Email
</div>

To change this to work with Umbraco, you can replace that part with something like:

// Get a reference to the Umbraco member service
IMemberService ms = UmbracoContext.Application.Services.MemberService;

// Generate a unique username for the user based on the ID of his/her Google account
string username = "google_" + user.Id;

// Get a reference to the member with that username (it may have been created yet)
IMember member = ms.GetByUsername(username);

// If not found, we need to create the member
if (member == null)  {

    // Generate a dummy email for the user
    string email = username + "@dummy.domain.com";

    // Create the member (this line will only create the member in memory)
    member = ms.CreateMemberWithIdentity(username, email, user.Name, "Member");

    // Save the member to the database
    ms.Save(member);

}

// Save a cookie that keeps the user logged in (false means for the session,
// true means as configured in your Web.config)
FormsAuthentication.SetAuthCookie(username, false);

// Redirect the user to a protected page
Response.Redirect("/your/member/section/");

A member in Umbraco must have an email address, which is why the example will generate a dummy email address. Depending on how your set up the authentication page, you will have the user's email form his/her Google account - in that case, your can use user.Email instead.

biapar commented 7 years ago

Hi, Thank you. Uhm, I think to authenticate a mobile user with its social login ( Google and so ), I've to create and save a token for the member and send to mobile application ( JWT ) and the Google token must be saved into a member "field" so we can check if the user was authorized or not .

Then Umbraco must be generate a JWT token used into the WebApi calls ( https://github.com/warrenbuckley/Umbraco-JWT-AuthTokens ).

My idea is to use social authentication to authorize an user that use a mobile app. I tried and configure an Umbraco test site with https://github.com/Shazwazza/UmbracoIdentity and works with social auth, but I'm study how to expand for the mobile use case or if more simple to use your lib.

abjerner commented 7 years ago

@biapar Something like that isn't supported by this library.

biapar commented 7 years ago

Uhm... Could help this http://24days.in/umbraco-cms/2015/umbraco-rest-api/?

abjerner commented 7 years ago

I don't really know - I haven't tried that.