Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AuthorizationCreate in Swift (Xcode 6)

I've been looking for some help in creating authorization for my app to have it run a few shell scripts as root. I've looked through the Apple documentation (which is of course written in OBJ-C and quite vague) and I'm trying to use the code examples in Swift.

Immediately I'm running in an error with the AuthorizationCreate function:

    var authRef: AuthorizationRef
    let osStatus = AuthorizationCreate(nil, nil, kAuthorizationFlagDefaults, &authRef)

'Int' is not convertible to 'AuthorizationFlags'

I'm just trying to follow along with the code snippets in the docs from: https://developer.apple.com/library/mac/documentation/Security/Conceptual/authorization_concepts/03authtasks/authtasks.html#//apple_ref/doc/uid/TP30000995-CH206-TP9

And I found the constant for kAuthorizationFlagDefaults from here: https://developer.apple.com/library/mac/documentation/Security/Reference/authorization_ref/#//apple_ref/doc/constant_group/Authorization_Options

I'm running in 10.10.1 if that matters.

I've seen the solution for using AppleScript, but I really want to avoid this is possible.

like image 709
Andrew Avatar asked Jun 28 '26 17:06

Andrew


1 Answers

kAuthorizationFlagDefaults is an Int and has to be converted to AuthorizationFlags (which is a type alias for UInt32). Also authRef has to be initialized:

var authRef: AuthorizationRef = nil
let authFlags = AuthorizationFlags(kAuthorizationFlagDefaults)
let osStatus = AuthorizationCreate(nil, nil, authFlags, &authRef)

Extended example (untested!):

var myItems = [
    AuthorizationItem(name: "com.myOrganization.myProduct.myRight1",
        valueLength: 0, value: nil, flags: 0),
    AuthorizationItem(name: "com.myOrganization.myProduct.myRight2",
        valueLength: 0, value: nil, flags: 0)
]

var myRights = AuthorizationRights(count: UInt32(myItems.count), items: &myItems)

let myFlags = AuthorizationFlags(kAuthorizationFlagDefaults |
                kAuthorizationFlagInteractionAllowed |
                kAuthorizationFlagExtendRights)


var authRef: AuthorizationRef = nil
let authFlags = AuthorizationFlags(kAuthorizationFlagDefaults)
let osStatus = AuthorizationCreate(&myRights, nil, authFlags, &authRef)

Edit: Swift 3

var myItems = [
        AuthorizationItem(name: "com.myOrganization.myProduct.myRight1",
                          valueLength: 0, value: nil, flags: 0),
        AuthorizationItem(name: "com.myOrganization.myProduct.myRight2",
                          valueLength: 0, value: nil, flags: 0)
    ]

    var myRights = AuthorizationRights(count: UInt32(myItems.count), items: &myItems)

    let myFlags : AuthorizationFlags = [.interactionAllowed, .extendRights]


    var authRef: AuthorizationRef?
    let osStatus = AuthorizationCreate(&myRights, nil, myFlags, &authRef)
like image 141
Martin R Avatar answered Jul 01 '26 13:07

Martin R



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!