Skip to content

Commit

Permalink
Login: added desktop param
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelbannov committed Oct 13, 2021
1 parent 3bb46b6 commit 66d1264
Show file tree
Hide file tree
Showing 14 changed files with 39 additions and 36 deletions.
10 changes: 8 additions & 2 deletions common/ASC.FederatedLogin/Helpers/OAuth20TokenHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public OAuth20TokenHelper(IHttpContextAccessor httpContextAccessor, ConsumerFact
ConsumerFactory = consumerFactory;
}

public string RequestCode<T>(string scope = null, Dictionary<string, string> additionalArgs = null) where T : Consumer, IOAuthProvider, new()
public string RequestCode<T>(bool desktop, string scope = null, Dictionary<string, string> additionalArgs = null) where T : Consumer, IOAuthProvider, new()
{
var loginProvider = ConsumerFactory.Get<T>();
var requestUrl = loginProvider.CodeUrl;
Expand All @@ -67,7 +67,13 @@ public OAuth20TokenHelper(IHttpContextAccessor httpContextAccessor, ConsumerFact
if (!string.IsNullOrEmpty(scope)) query += $"&scope={HttpUtility.UrlEncode(scope)}";

var u = HttpContextAccessor.HttpContext.Request.GetUrlRewriter();
var state = HttpUtility.UrlEncode(new UriBuilder(u.Scheme, u.Host, u.Port, $"thirdparty/{loginProvider.Name.ToLower()}/code").Uri.AbsoluteUri);
var stateUriBuilder = new UriBuilder(u.Scheme, u.Host, u.Port, $"thirdparty/{loginProvider.Name.ToLower()}/code");
if (desktop)
{
stateUriBuilder.Query = "desktop=true";
}

var state = HttpUtility.UrlEncode(stateUriBuilder.Uri.AbsoluteUri);
query += $"&state={state}";

if (additionalArgs != null)
Expand Down
2 changes: 1 addition & 1 deletion common/ASC.FederatedLogin/Login.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public async Task Invoke(HttpContext context)
{
try
{
var profile = ProviderManager.Process(Auth, context, _params);
var profile = ProviderManager.Process(Auth, context, _params.ContainsKey("desktop") && _params["desktop"] == "true", _params);
if (profile != null)
{
await SendClientData(context, profile);
Expand Down
8 changes: 4 additions & 4 deletions common/ASC.FederatedLogin/LoginProviders/BaseLoginProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,11 @@ protected BaseLoginProvider(
InstanceCrypto = instanceCrypto;
}

public virtual LoginProfile ProcessAuthoriztion(HttpContext context, IDictionary<string, string> @params)
public virtual LoginProfile ProcessAuthoriztion(HttpContext context, bool desktop, IDictionary<string, string> @params)
{
try
{
var token = Auth(context, Scopes, out var redirect);
var token = Auth(context, desktop, Scopes, out var redirect);

if (redirect)
{
Expand All @@ -136,7 +136,7 @@ public virtual LoginProfile ProcessAuthoriztion(HttpContext context, IDictionary
}
}

protected virtual OAuth20Token Auth(HttpContext context, string scopes, out bool redirect, Dictionary<string, string> additionalArgs = null)
protected virtual OAuth20Token Auth(HttpContext context, bool desktop, string scopes, out bool redirect, Dictionary<string, string> additionalArgs = null)
{
var error = context.Request.Query["error"];
if (!string.IsNullOrEmpty(error))
Expand All @@ -151,7 +151,7 @@ protected virtual OAuth20Token Auth(HttpContext context, string scopes, out bool
var code = context.Request.Query["code"];
if (string.IsNullOrEmpty(code))
{
context.Response.Redirect(OAuth20TokenHelper.RequestCode<T>(scopes, additionalArgs));
context.Response.Redirect(OAuth20TokenHelper.RequestCode<T>(desktop, scopes, additionalArgs));
redirect = true;
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public override LoginProfile GetLoginProfile(string accessToken)

public OAuth20Token Auth(HttpContext context)
{
return Auth(context, GoogleScopeContacts, out var _, (context.Request.Query["access_type"].ToString() ?? "") == "offline"
return Auth(context, context.Request.Query["desktop"] == "true", GoogleScopeContacts, out var _, (context.Request.Query["access_type"].ToString() ?? "") == "offline"
? new Dictionary<string, string>
{
{ "access_type", "offline" },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,11 @@ public GosUslugiLoginProvider(
}


public override LoginProfile ProcessAuthoriztion(HttpContext context, IDictionary<string, string> @params)
public override LoginProfile ProcessAuthoriztion(HttpContext context, bool desktop, IDictionary<string, string> @params)
{
try
{
var token = Auth(context, Scopes, out var redirect);
var token = Auth(context, desktop, Scopes, out var redirect);

if (redirect)
{
Expand All @@ -142,7 +142,7 @@ public override LoginProfile ProcessAuthoriztion(HttpContext context, IDictionar
}
}

protected override OAuth20Token Auth(HttpContext context, string scopes, out bool redirect, Dictionary<string, string> additionalArgs = null)
protected override OAuth20Token Auth(HttpContext context, bool desktop, string scopes, out bool redirect, Dictionary<string, string> additionalArgs = null)
{
var error = context.Request.Query["error"];
if (!string.IsNullOrEmpty(error))
Expand Down
2 changes: 1 addition & 1 deletion common/ASC.FederatedLogin/LoginProviders/ILoginProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ namespace ASC.FederatedLogin.LoginProviders
{
public interface ILoginProvider : IOAuthProvider
{
LoginProfile ProcessAuthoriztion(HttpContext context, IDictionary<string, string> @params);
LoginProfile ProcessAuthoriztion(HttpContext context, bool desktop, IDictionary<string, string> @params);

LoginProfile GetLoginProfile(string accessToken);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,11 @@ public MailRuLoginProvider(
{
}

public override LoginProfile ProcessAuthoriztion(HttpContext context, IDictionary<string, string> @params)
public override LoginProfile ProcessAuthoriztion(HttpContext context, bool desktop, IDictionary<string, string> @params)
{
try
{
var token = Auth(context, Scopes, out var redirect);
var token = Auth(context, desktop, Scopes, out var redirect);

if (redirect)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public OpenIdLoginProvider(Signature signature, InstanceCrypto instanceCrypto, C
ConsumerFactory = consumerFactory;
}

public LoginProfile ProcessAuthoriztion(HttpContext context, IDictionary<string, string> @params)
public LoginProfile ProcessAuthoriztion(HttpContext context, bool desktop, IDictionary<string, string> @params)
{
var response = Openid.GetResponse();
if (response == null)
Expand Down
4 changes: 2 additions & 2 deletions common/ASC.FederatedLogin/LoginProviders/ProviderManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ public ILoginProvider GetLoginProvider(string providerType)
: ConsumerFactory.GetByKey(providerType) as ILoginProvider;
}

public LoginProfile Process(string providerType, HttpContext context, IDictionary<string, string> @params)
public LoginProfile Process(string providerType, HttpContext context, bool desktop, IDictionary<string, string> @params)
{
return GetLoginProvider(providerType).ProcessAuthoriztion(context, @params);
return GetLoginProvider(providerType).ProcessAuthoriztion(context, desktop, @params);
}

public LoginProfile GetLoginProfile(string providerType, string accessToken)
Expand Down
4 changes: 2 additions & 2 deletions common/ASC.FederatedLogin/LoginProviders/VKLoginProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,11 @@ public VKLoginProvider(
}


public override LoginProfile ProcessAuthoriztion(HttpContext context, IDictionary<string, string> @params)
public override LoginProfile ProcessAuthoriztion(HttpContext context, bool desktop, IDictionary<string, string> @params)
{
try
{
var token = Auth(context, Scopes, out var redirect, context.Request.Query["access_type"] == "offline"
var token = Auth(context, desktop, Scopes, out var redirect, context.Request.Query["access_type"] == "offline"
? new Dictionary<string, string>
{
{ "revoke", "1" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public YahooLoginProvider(

public OAuth20Token Auth(HttpContext context)
{
return Auth(context, Scopes, out var _);
return Auth(context, context.Request.Query["desktop"] == "true", Scopes, out var _);
}

public override LoginProfile GetLoginProfile(string accessToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@ public YandexLoginProvider(
{
}

public override LoginProfile ProcessAuthoriztion(HttpContext context, IDictionary<string, string> @params)
public override LoginProfile ProcessAuthoriztion(HttpContext context, bool desktop, IDictionary<string, string> @params)
{
try
{
var token = Auth(context, Scopes, out var redirect, context.Request.Query["access_type"] == "offline"
var token = Auth(context, desktop, Scopes, out var redirect, context.Request.Query["access_type"] == "offline"
? new Dictionary<string, string>
{
{ "force_confirm", "true" }
Expand Down
11 changes: 3 additions & 8 deletions products/ASC.People/Server/Controllers/PeopleController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1611,14 +1611,9 @@ public ICollection<AccountInfo> GetAuthProviders(bool inviteView, bool settingsV
{

var url = VirtualPathUtility.ToAbsolute("~/login.ashx") + $"?auth={provider}";
var mode = (settingsView || inviteView || (!MobileDetector.IsMobile() && !Request.DesktopApp())
? ("&mode=popup&callback=" + clientCallback)
: ("&mode=Redirect&returnurl="
+ HttpUtility.UrlEncode(new Uri(Request.GetUrlRewriter(),
"Auth.aspx"
+ (Request.DesktopApp() ? "?desktop=true" : "")
).ToString())
));
var mode = settingsView || inviteView || (!MobileDetector.IsMobile() && !Request.DesktopApp())
? $"&mode=popup&callback={clientCallback}"
: "&mode=Redirect&desktop=true";

infos.Add(new AccountInfo
{
Expand Down
14 changes: 8 additions & 6 deletions web/ASC.Web.Api/Controllers/ThirdPartyController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,11 @@ public ThirdPartyController(OAuth20TokenHelper oAuth20TokenHelper)
[Read("{provider}")]
public object Get(LoginProviderEnum provider)
{
var desktop = HttpContext.Request.Query["desktop"] == "true";
switch (provider)
{
case LoginProviderEnum.Google:
return OAuth20TokenHelper.RequestCode<GoogleLoginProvider>(
return OAuth20TokenHelper.RequestCode<GoogleLoginProvider>(desktop,
GoogleLoginProvider.GoogleScopeDrive,
new Dictionary<string, string>
{
Expand All @@ -64,26 +65,27 @@ public object Get(LoginProviderEnum provider)
});

case LoginProviderEnum.Dropbox:
return OAuth20TokenHelper.RequestCode<DropboxLoginProvider>(
return OAuth20TokenHelper.RequestCode<DropboxLoginProvider>(desktop,
additionalArgs: new Dictionary<string, string>
{
{ "force_reauthentication", "true" }
});

case LoginProviderEnum.Docusign:
return OAuth20TokenHelper.RequestCode<DocuSignLoginProvider>(DocuSignLoginProvider.DocuSignLoginProviderScopes,
return OAuth20TokenHelper.RequestCode<DocuSignLoginProvider>(desktop,
DocuSignLoginProvider.DocuSignLoginProviderScopes,
new Dictionary<string, string>
{
{ "prompt", "login" }
});
case LoginProviderEnum.Box:
return OAuth20TokenHelper.RequestCode<BoxLoginProvider>();
return OAuth20TokenHelper.RequestCode<BoxLoginProvider>(desktop);

case LoginProviderEnum.OneDrive:
return OAuth20TokenHelper.RequestCode<OneDriveLoginProvider>(OneDriveLoginProvider.OneDriveLoginProviderScopes);
return OAuth20TokenHelper.RequestCode<OneDriveLoginProvider>(desktop, OneDriveLoginProvider.OneDriveLoginProviderScopes);

case LoginProviderEnum.Wordpress:
return OAuth20TokenHelper.RequestCode<WordpressLoginProvider>();
return OAuth20TokenHelper.RequestCode<WordpressLoginProvider>(desktop);

}

Expand Down

0 comments on commit 66d1264

Please sign in to comment.