2013-04-11 11 views
5

Pracuję z ServiceStack i jego dostawcami autoryzacji. W szczególności "FacebookAuthProvider". Mój problem polega na tym, że usługa jest wywoływana z aplikacji na iOS. Ta aplikacja ma już ważny token dostępu i chcę po prostu przekazać tę wartość do uwierzytelniania facebook servicestack.ServiceStack i FacebookAuthProvider

Widziałem testy strony gitubu servicestack, ale nadal nie ma to dla mnie sensu.

Czy można przekazać token dostępu do serwicestru, więc uwierzytelnienie pomija część, w której pytam o pozwolenie, ponieważ już zrobiliśmy to w aplikacji?

A może to niewłaściwy sposób?

+0

Czy znalazłeś sposób, aby to zrobić? – fractal

Odpowiedz

3

Zamiast korzystać z wbudowanego dostawcy uwierzytelnień Facebook, utworzyłem własny CustomFacebookAuthProvider. Powodem jest to, że wbudowana wersja wymaga przeglądarki, aby przekierować użytkownika do Facebooka w celu uwierzytelnienia i nie potrzebowałem tego. Miałem już token dostępu.

Tak więc na podstawie oficjalnej wersji FacebookAuthProvider.cs stworzyłem własne.

using System; 
using System.Collections.Generic; 
using System.Net; 
using Elmah; 
using Mondohunter.Backend.BusinessLogic.Interfaces; 
using ServiceStack.Common.Extensions; 
using ServiceStack.Common.Web; 
using ServiceStack.Configuration; 
using ServiceStack.ServiceInterface; 
using ServiceStack.ServiceInterface.Auth; 
using ServiceStack.Text; 
using ServiceStack.WebHost.Endpoints; 

namespace Mondohunter.Interfaces 
{ 
    public class CustomFacebookAuthProvider : OAuthProvider 
    { 
     public const string Name = "facebook"; 
     public static string Realm = "https://graph.facebook.com/"; 
     public static string PreAuthUrl = "https://www.facebook.com/dialog/oauth"; 

     public string AppId { get; set; } 
     public string AppSecret { get; set; } 
     public string[] Permissions { get; set; } 

     public CustomFacebookAuthProvider(IResourceManager appSettings) 
      : base(appSettings, Realm, Name, "AppId", "AppSecret") 
     { 
      this.AppId = appSettings.GetString("oauth.facebook.AppId"); 
      this.AppSecret = appSettings.GetString("oauth.facebook.AppSecret"); 
     } 

     public override object Authenticate(IServiceBase authService, IAuthSession session, Auth request) 
     { 
      var tokens = Init(authService, ref session, request); 

      try 
      { 
       if (request.oauth_token.IsNullOrEmpty()) 
        throw new Exception(); 

       tokens.AccessToken = request.oauth_token; 
       session.IsAuthenticated = true; 

       var json = AuthHttpGateway.DownloadFacebookUserInfo(request.oauth_token); 
       var authInfo = JsonSerializer.DeserializeFromString<Dictionary<string, string>>(json); 

       //Here i need to update/set userauth id to the email 
       //UpdateUserAuthId(session, authInfo["email"]); 

       authService.SaveSession(session, SessionExpiry); 
       OnAuthenticated(authService, session, tokens, authInfo); 

       //return json/xml/... response; 
      } 
      catch (WebException ex) 
      { 
       //return json/xml/... response; 
      } 
      catch (Exception ex) 
      { 
       //return json/xml/... response; 
      } 
     } 

     protected override void LoadUserAuthInfo(AuthUserSession userSession, IOAuthTokens tokens, Dictionary<string, string> authInfo) 
     { 
      if (authInfo.ContainsKey("id")) 
       tokens.UserId = authInfo.GetValueOrDefault("id"); 
      if (authInfo.ContainsKey("name")) 
       tokens.DisplayName = authInfo.GetValueOrDefault("name"); 
      if (authInfo.ContainsKey("first_name")) 
       tokens.FirstName = authInfo.GetValueOrDefault("first_name"); 
      if (authInfo.ContainsKey("last_name")) 
       tokens.LastName = authInfo.GetValueOrDefault("last_name"); 
      if (authInfo.ContainsKey("email")) 
       tokens.Email = authInfo.GetValueOrDefault("email"); 
      if (authInfo.ContainsKey("gender")) 
       tokens.Gender = authInfo.GetValueOrDefault("gender"); 
      if (authInfo.ContainsKey("timezone")) 
       tokens.TimeZone = authInfo.GetValueOrDefault("timezone"); 

      LoadUserOAuthProvider(userSession, tokens); 
     } 

     public override void LoadUserOAuthProvider(IAuthSession authSession, IOAuthTokens tokens) 
     { 
      var userSession = authSession as CustomUserSession; 
      if (userSession == null) return; 

      userSession.Email = tokens.Email ?? userSession.PrimaryEmail ?? userSession.Email; 
     } 
    } 
} 

Mam nadzieję, że to ma sens.

+0

Allan, jak zwrócisz jsona, możesz podać przykład? Ponadto, jak skonfigurować adres URL, aby uzyskać dostęp do dostawcy auth, na przykład dla wbudowanego masz "/ auth/facebook". – pug

Powiązane problemy