]> err.no Git - sope/blob - xmlrpc_call/HandleCredentialsClient.m
more OSX framework stuff
[sope] / xmlrpc_call / HandleCredentialsClient.m
1 /*
2   Copyright (C) 2004 Helge Hess
3
4   This file is part of SOPE.
5
6   SOPE is free software; you can redistribute it and/or modify it under
7   the terms of the GNU Lesser General Public License as published by the
8   Free Software Foundation; either version 2, or (at your option) any
9   later version.
10
11   SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
12   WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14   License for more details.
15
16   You should have received a copy of the GNU Lesser General Public
17   License along with SOPE; see the file COPYING.  If not, write to the
18   Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19   02111-1307, USA.
20 */
21
22 #include "HandleCredentialsClient.h"
23 #include <NGObjWeb/WOResponse.h>
24 #include "common.h"
25 #include <unistd.h>
26
27 @interface NGXmlRpcClient(CallFailed)
28 - (id)callFailed:(WOResponse *)_response;
29 @end /* NGXmlRpcClient */
30
31 @implementation HandleCredentialsClient
32
33 - (void)dealloc {
34   [self->defLogin    release];
35   [self->defPassword release];
36   [super dealloc];
37 }
38
39 /* accessors */
40
41 - (void)setDefLogin:(NSString *)_login {
42   ASSIGNCOPY(self->defLogin, _login);
43 }
44 - (void)setDefPassword:(NSString *)_pwd {
45   ASSIGNCOPY(self->defPassword, _pwd);
46 }
47
48 /* prompting */
49
50 - (NSString *)prompt:(NSString *)_prompt {
51   NSString *login;
52   char clogin[256];
53   
54   fprintf(stderr, "%s", [_prompt cString]);
55   fflush(stderr);
56   fgets(clogin, 200, stdin);
57   clogin[strlen(clogin) - 1] = '\0';
58   login = [NSString stringWithCString:clogin];
59   return login;
60 }
61
62 - (NSString *)promptPassword:(NSString *)_prompt {
63   NSString *pwd;
64   char     *cpwd;
65
66   cpwd = getpass("password: ");
67   pwd = [NSString stringWithCString:cpwd];
68   return pwd;
69 }
70
71 - (id)callFailed:(WOResponse *)_response {
72   if ([_response status] == 401) {
73     NSString *wwwauth;
74     NSString *user;
75     NSString *pass;
76     
77     wwwauth = [_response headerForKey:@"www-authenticate"];
78     if ([[wwwauth lowercaseString] hasPrefix:@"digest"])
79       [self logWithFormat:@"Digest authentication:\n'%@'", wwwauth];
80
81     // TODO: test credentials of URL
82     
83     if (self->defLogin) {
84       user = [self->defLogin autorelease];
85       self->defLogin = nil;
86     }
87     else
88       user = [self prompt:@"login:    "];
89     
90     if (self->defPassword) {
91       pass = [self->defPassword autorelease];
92       self->defPassword = nil;
93     }
94     else
95       pass = [self promptPassword:@"password: "];
96     
97     [self setUserName:user];
98     [self setPassword:pass];
99     
100     /* this "should" return some kind of "need-pwd" object ... */
101     return nil;
102   }
103   else {
104     return [super callFailed:_response];
105   }
106 }
107
108 @end /* HandleCredentialsClient */