-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
226 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<packages> | ||
<package id="Microsoft.CSharp" version="4.0.1" targetFramework="xamarinmac20" /> | ||
<package id="Newtonsoft.Json" version="9.0.1" targetFramework="xamarinmac20" /> | ||
<package id="System.Collections" version="4.0.11" targetFramework="xamarinmac20" /> | ||
<package id="System.Diagnostics.Debug" version="4.0.11" targetFramework="xamarinmac20" /> | ||
<package id="System.Dynamic.Runtime" version="4.0.11" targetFramework="xamarinmac20" /> | ||
<package id="System.Globalization" version="4.0.11" targetFramework="xamarinmac20" /> | ||
<package id="System.IO" version="4.1.0" targetFramework="xamarinmac20" /> | ||
<package id="System.Linq" version="4.1.0" targetFramework="xamarinmac20" /> | ||
<package id="System.Linq.Expressions" version="4.1.0" targetFramework="xamarinmac20" /> | ||
<package id="System.ObjectModel" version="4.0.12" targetFramework="xamarinmac20" /> | ||
<package id="System.Reflection" version="4.1.0" targetFramework="xamarinmac20" /> | ||
<package id="System.Reflection.Extensions" version="4.0.1" targetFramework="xamarinmac20" /> | ||
<package id="System.Resources.ResourceManager" version="4.0.1" targetFramework="xamarinmac20" /> | ||
<package id="System.Runtime" version="4.1.0" targetFramework="xamarinmac20" /> | ||
<package id="System.Runtime.Extensions" version="4.1.0" targetFramework="xamarinmac20" /> | ||
<package id="System.Runtime.Serialization.Primitives" version="4.1.1" targetFramework="xamarinmac20" /> | ||
<package id="System.Text.Encoding" version="4.0.11" targetFramework="xamarinmac20" /> | ||
<package id="System.Text.Encoding.Extensions" version="4.0.11" targetFramework="xamarinmac20" /> | ||
<package id="System.Text.RegularExpressions" version="4.1.0" targetFramework="xamarinmac20" /> | ||
<package id="System.Threading" version="4.0.11" targetFramework="xamarinmac20" /> | ||
<package id="System.Threading.Tasks" version="4.0.11" targetFramework="xamarinmac20" /> | ||
<package id="System.Xml.ReaderWriter" version="4.0.11" targetFramework="xamarinmac20" /> | ||
<package id="System.Xml.XDocument" version="4.0.11" targetFramework="xamarinmac20" /> | ||
</packages> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// | ||
// Copyright 2016 Clancey | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
using System; | ||
using Foundation; | ||
|
||
namespace Foundation | ||
{ | ||
public static class AppleEventParameters | ||
{ | ||
static uint FourCC (string s) | ||
{ | ||
return (uint)(((int)s [0]) << 24 | | ||
((int)s [1]) << 16 | | ||
((int)s [2]) << 8 | | ||
((int)s [3])); | ||
} | ||
|
||
|
||
public static uint DirectObject => FourCC (keyDirectObject); | ||
public static uint ErrorNumber => FourCC (keyErrorNumber); | ||
public static uint ErrorString => FourCC (keyErrorString); | ||
public static uint ProcessSerialNumber => FourCC (keyProcessSerialNumber); | ||
public static uint PreDispatch => FourCC (keyPreDispatch); | ||
public static uint SelectProc => FourCC (keySelectProc); | ||
public static uint AERecorderCount => FourCC (keyAERecorderCount); | ||
public static uint AEVersion => FourCC (keyAEVersion); | ||
|
||
|
||
/* Keywords for Apple event parameters */ | ||
const string keyDirectObject = "----"; | ||
const string keyErrorNumber = "errn"; | ||
const string keyErrorString = "errs"; | ||
const string keyProcessSerialNumber = "psn "; /* Keywords for special handlers */ | ||
const string keyPreDispatch = "phac"; /* preHandler accessor call */ | ||
const string keySelectProc = "selh"; /* more selector call */ | ||
/* Keyword for recording */ | ||
const string keyAERecorderCount = "recr"; /* available only in vers 1.0.1 and greater */ | ||
/* Keyword for version information */ | ||
const string keyAEVersion = "vers"; /* available only in vers 1.0.1 and greater */ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
// | ||
// Copyright 2016 Clancey | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
using System; | ||
using System.Collections.Generic; | ||
using Foundation; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using System.Web; | ||
using AppKit; | ||
|
||
namespace SimpleAuth | ||
{ | ||
public class NativeSafariAuthenticator | ||
{ | ||
public NativeSafariAuthenticator () | ||
{ | ||
} | ||
const string CFBundleUrlError = "CFBundleURLSchemes are required for Native Safari Auth"; | ||
static Dictionary<string, WebAuthenticator> authenticators = new Dictionary<string, WebAuthenticator> (); | ||
public static void Activate () | ||
{ | ||
if (!GetCFBundleURLSchemes ().Any ()) | ||
throw new Exception (CFBundleUrlError); | ||
|
||
OAuthApi.ShowAuthenticator = ShowAuthenticator; | ||
} | ||
public static void ShowAuthenticator (WebAuthenticator authenticator) | ||
{ | ||
var urls = GetCFBundleURLSchemes (); | ||
if (!urls.Any ()) { | ||
authenticator.OnError (CFBundleUrlError); | ||
return; | ||
} | ||
|
||
//TODO: validate the proper url is in there | ||
|
||
var invoker = new Foundation.NSObject (); | ||
invoker.BeginInvokeOnMainThread (async () => await BeginAuthentication( authenticator)); | ||
} | ||
|
||
static async Task BeginAuthentication (WebAuthenticator authenticator) | ||
{ | ||
try { | ||
var uri = (await authenticator.GetInitialUrl ()); | ||
string redirectUrl = uri.GetParameter ("redirect_uri"); | ||
var scheme = new Uri (redirectUrl).Scheme; | ||
if (!VerifyHasUrlScheme(scheme)) { | ||
authenticator.OnError ($"Unable to redirect {redirectUrl}, Please add the Url Scheme to the info.plist"); | ||
return; | ||
} | ||
var url = new NSUrl (uri.AbsoluteUri); | ||
var opened = NSWorkspace.SharedWorkspace.OpenUrl (url); | ||
if (!opened) | ||
authenticator.OnError ("Error opening Safari"); | ||
else | ||
authenticators [scheme] = authenticator; | ||
} catch (Exception ex) { | ||
authenticator.OnError (ex.Message); | ||
} | ||
} | ||
|
||
static bool VerifyHasUrlScheme (string scheme) | ||
{ | ||
var schemes = GetCFBundleURLSchemes (); | ||
return schemes.Any (x => x == scheme); | ||
} | ||
static string [] GetCFBundleURLSchemes () | ||
{ | ||
NSObject nsobj; | ||
if (!NSBundle.MainBundle.InfoDictionary.TryGetValue ((NSString)"CFBundleURLTypes", out nsobj)) | ||
return new string [0]; | ||
var d = (nsobj as NSArray)?.GetItem<NSDictionary> (0); | ||
if (!d?.TryGetValue ((NSString)"CFBundleURLSchemes", out nsobj) ?? false) | ||
return new string [0]; | ||
var a = nsobj as NSArray; | ||
var urls = ConvertToIEnumerable<NSString> (a).Select (x => x.ToString ()).ToArray (); | ||
return urls; | ||
} | ||
|
||
static IEnumerable<T> ConvertToIEnumerable<T> (NSArray array) where T : class, ObjCRuntime.INativeObject | ||
{ | ||
for (nuint i = 0; i < array.Count; i++) { | ||
yield return array.GetItem<T> (i); | ||
} | ||
|
||
} | ||
public static bool ResumeAuth (string url) | ||
{ | ||
try { | ||
var uri = new Uri (url); | ||
var redirect = uri.Scheme; | ||
var auth = authenticators [redirect]; | ||
var s = auth.CheckUrl (uri, new System.Net.Cookie [0]); | ||
if (s) { | ||
authenticators.Remove (redirect); | ||
} | ||
return s; | ||
|
||
} catch (Exception ex) { | ||
Console.WriteLine (ex); | ||
} | ||
return false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters