Instantly share code, notes, and snippets.
-
Save abock/4d37b8a7511fd99cbe2ef874436ffafd to your computer and use it in GitHub Desktop.
SFAuthorization
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
// | |
// SFAuthorization.cs | |
// | |
// Author: | |
// Aaron Bockover <abock@xamarin.com> | |
// | |
// Copyright 2017 Microsoft. All rights reserved. | |
using System; | |
using Foundation; | |
using Security; | |
namespace SecurityFoundation | |
{ | |
// @interface SFAuthorization : NSObject <NSCoding> | |
[BaseType (typeof (NSObject))] | |
interface SFAuthorization : INSCoding | |
{ | |
// +(id)authorization; | |
[Static] | |
[Export ("authorization")] | |
SFAuthorization Authorization { get; } | |
// -(AuthorizationRef)authorizationRef; | |
// TODO: This should return a `Security.Authorization` instead, but the | |
// ref is owned by this `SFAuthorization`. `Security.Authorization` needs | |
// a .ctor that does allows the wrapper to not take ownership of the ref. | |
[Internal] | |
[Export ("authorizationRef")] | |
IntPtr AuthorizationRef { get; } | |
// +(id)authorizationWithFlags:(AuthorizationFlags)flags rights:(const AuthorizationRights *)rights environment:(const AuthorizationEnvironment *)environment; | |
[Static] | |
[Internal] | |
[Export ("authorizationWithFlags:rights:environment:")] | |
SFAuthorization AuthorizationWithFlags (AuthorizationFlags flags, IntPtr rights, IntPtr environment); | |
// -(id)initWithFlags:(AuthorizationFlags)flags rights:(const AuthorizationRights *)rights environment:(const AuthorizationEnvironment *)environment; | |
[Internal] | |
[Export ("initWithFlags:rights:environment:")] | |
IntPtr Constructor (AuthorizationFlags flags, IntPtr rights, IntPtr environment); | |
// -(void)invalidateCredentials; | |
[Export ("invalidateCredentials")] | |
void InvalidateCredentials (); | |
// -(BOOL)obtainWithRight:(AuthorizationString)rightName flags:(AuthorizationFlags)flags error:(NSError **)error; | |
[Internal] | |
[Export ("obtainWithRight:flags:error:")] | |
bool ObtainWithRight (IntPtr rightName, AuthorizationFlags flags, out NSError error); | |
// -(BOOL)obtainWithRights:(const AuthorizationRights *)rights flags:(AuthorizationFlags)flags environment:(const AuthorizationEnvironment *)environment authorizedRights:(AuthorizationRights **)authorizedRights error:(NSError **)error; | |
[Internal] | |
[Export ("obtainWithRights:flags:environment:authorizedRights:error:")] | |
bool ObtainWithRights (IntPtr rights, AuthorizationFlags flags, IntPtr environment, IntPtr authorizedRights, out NSError error); | |
} | |
} |
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
// | |
// SFAuthorization_Extensions.cs | |
// | |
// Author: | |
// Aaron Bockover <abock@xamarin.com> | |
// | |
// Copyright 2017 Microsoft. All rights reserved. | |
using System; | |
using System.Text; | |
using System.Runtime.InteropServices; | |
using Foundation; | |
using Security; | |
namespace SecurityFoundation | |
{ | |
partial class SFAuthorization | |
{ | |
// TODO: wrappers for the other internal rights APIs | |
public unsafe void ObtainWithRight (string rightName, AuthorizationFlags flags, out NSError error) | |
{ | |
if (rightName == null) | |
throw new ArgumentNullException (nameof (rightName)); | |
var utf8 = new byte [Encoding.UTF8.GetByteCount (rightName) + 1]; | |
Encoding.UTF8.GetBytes (rightName, 0, rightName.Length, utf8, 0); | |
fixed (byte* utf8Pointer = utf8) { | |
if (!ObtainWithRight (new IntPtr (utf8Pointer), flags, out error)) | |
throw new NSErrorException (error); | |
} | |
} | |
const int kAuthorizationExternalFormLength = 32; | |
[DllImport (ObjCRuntime.Constants.SecurityLibrary)] | |
static extern int AuthorizationMakeExternalForm (IntPtr authorizationRef, IntPtr extForm); | |
// FIXME: there should be a `Security.Authorization.MakeExternalForm`, and this | |
// helper should simply exist as `NSData MakeExternalForm () => Authorization.MakeExternalForm ()`; | |
// see the note in the `SFAuthorization` binding scaffolding. | |
public NSData MakeExternalForm () | |
{ | |
var extForm = Marshal.AllocHGlobal (kAuthorizationExternalFormLength); | |
var result = AuthorizationMakeExternalForm (AuthorizationRef, extForm); | |
if (result == 0) | |
return NSData.FromBytesNoCopy (extForm, kAuthorizationExternalFormLength, true); | |
Marshal.FreeHGlobal (extForm); | |
throw new Exception ($"AuthorizationMakeExternalForm returned {result}"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment