Skip to content

Instantly share code, notes, and snippets.

@abock abock/SFAuthorization.cs Secret
Last active Aug 7, 2017

Embed
What would you like to do?
SFAuthorization
//
// 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);
}
}
//
// 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
You can’t perform that action at this time.