forked from libgit2/objective-git
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathGTCredential.m
More file actions
123 lines (90 loc) · 4.46 KB
/
GTCredential.m
File metadata and controls
123 lines (90 loc) · 4.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
//
// GTCredential.m
// ObjectiveGitFramework
//
// Created by Etienne on 10/09/13.
// Copyright (c) 2013 GitHub, Inc. All rights reserved.
//
#import "GTCredential.h"
#import "GTCredential+Private.h"
#import "NSError+Git.h"
#import "git2/errors.h"
typedef GTCredential *(^GTCredentialProviderBlock)(GTCredentialType allowedTypes, NSString *URL, NSString *userName);
@interface GTCredentialProvider ()
@property (nonatomic, readonly, copy) GTCredentialProviderBlock credBlock;
@end
@implementation GTCredentialProvider
+ (instancetype)providerWithBlock:(GTCredentialProviderBlock)credentialBlock {
NSParameterAssert(credentialBlock != nil);
GTCredentialProvider *provider = [[self alloc] init];
provider->_credBlock = [credentialBlock copy];
return provider;
}
- (GTCredential *)credentialForType:(GTCredentialType)type URL:(NSString *)URL userName:(NSString *)userName {
NSAssert(self.credBlock != nil, @"Provider asked for credentials without block being set.");
return self.credBlock(type, URL, userName);
}
@end
@interface GTCredential ()
@property (nonatomic, assign, readonly) git_cred *git_cred;
@end
@implementation GTCredential
+ (instancetype)credentialWithUserName:(NSString *)userName password:(NSString *)password error:(NSError **)error {
git_cred *cred;
int gitError = git_cred_userpass_plaintext_new(&cred, userName.UTF8String, password.UTF8String);
if (gitError != GIT_OK) {
if (error) *error = [NSError git_errorFor:gitError description:@"Failed to create credentials object" failureReason:@"There was an error creating a credential object for username %@.", userName];
return nil;
}
return [[self alloc] initWithGitCred:cred];
}
+ (instancetype)credentialWithUserName:(NSString *)userName publicKeyURL:(NSURL *)publicKeyURL privateKeyURL:(NSURL *)privateKeyURL passphrase:(NSString *)passphrase error:(NSError **)error {
NSParameterAssert(privateKeyURL != nil);
NSString *publicKeyPath = publicKeyURL.filePathURL.path;
NSString *privateKeyPath = privateKeyURL.filePathURL.path;
NSAssert(privateKeyPath != nil, @"Invalid file URL passed: %@", privateKeyURL);
git_cred *cred;
int gitError = git_cred_ssh_key_new(&cred, userName.UTF8String, publicKeyPath.fileSystemRepresentation, privateKeyPath.fileSystemRepresentation, passphrase.UTF8String);
if (gitError != GIT_OK) {
if (error) *error = [NSError git_errorFor:gitError description:@"Failed to create credentials object" failureReason:@"There was an error creating a credential object for username %@ with the provided public/private key pair.\nPublic key: %@\nPrivate key: %@", userName, publicKeyURL, privateKeyURL];
return nil;
}
return [[self alloc] initWithGitCred:cred];
}
+ (instancetype)credentialWithUserName:(NSString *)userName publicKeyString:(NSString *)publicKeyString privateKeyString:(NSString *)privateKeyString passphrase:(NSString *)passphrase error:(NSError **)error {
NSParameterAssert(privateKeyString != nil);
git_cred *cred;
int gitError = git_cred_ssh_key_memory_new(&cred, userName.UTF8String, publicKeyString.UTF8String, privateKeyString.UTF8String, passphrase.UTF8String);
if (gitError != GIT_OK) {
if (error) *error = [NSError git_errorFor:gitError description:@"Failed to create credentials object" failureReason:@"There was an error creating a credential object for username %@ with the provided public/private key pair.\nPublic key: %@", userName, publicKeyString];
return nil;
}
return [[self alloc] initWithGitCred:cred];
}
- (instancetype)initWithGitCred:(git_cred *)cred {
NSParameterAssert(cred != nil);
self = [self init];
if (self == nil) return nil;
_git_cred = cred;
return self;
}
@end
int GTCredentialAcquireCallback(git_cred **git_cred, const char *url, const char *username_from_url, unsigned int allowed_types, void *payload) {
NSCParameterAssert(git_cred != NULL);
NSCParameterAssert(payload != NULL);
GTCredentialAcquireCallbackInfo *info = payload;
GTCredentialProvider *provider = info->credProvider;
if (provider == nil) {
git_error_set_str(GIT_EUSER, "No GTCredentialProvider set, but authentication was requested.");
return GIT_ERROR;
}
NSString *URL = (url != NULL ? @(url) : @"");
NSString *userName = (username_from_url != NULL ? @(username_from_url) : nil);
GTCredential *cred = [provider credentialForType:(GTCredentialType)allowed_types URL:URL userName:userName];
if (cred == nil) {
git_error_set_str(GIT_EUSER, "GTCredentialProvider failed to provide credentials.");
return GIT_ERROR;
}
*git_cred = cred.git_cred;
return GIT_OK;
}