]> err.no Git - sope/blob - xmlrpc_call/HandleCredentialsClient.m
fixed gstep-base warnings
[sope] / xmlrpc_call / HandleCredentialsClient.m
1 /*
2   Copyright (C) 2004-2005 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 <string.h>
26 #include <unistd.h>
27
28 @interface NGXmlRpcClient(CallFailed)
29 - (id)callFailed:(WOResponse *)_response;
30 @end /* NGXmlRpcClient */
31
32 @implementation HandleCredentialsClient
33
34 - (void)dealloc {
35   [self->defLogin    release];
36   [self->defPassword release];
37   [super dealloc];
38 }
39
40 /* accessors */
41
42 - (void)setDefLogin:(NSString *)_login {
43   ASSIGNCOPY(self->defLogin, _login);
44 }
45 - (void)setDefPassword:(NSString *)_pwd {
46   ASSIGNCOPY(self->defPassword, _pwd);
47 }
48
49 /* prompting */
50
51 - (NSString *)prompt:(NSString *)_prompt {
52   NSString *login;
53   char clogin[256];
54   
55   fprintf(stderr, "%s", [_prompt cString]);
56   fflush(stderr);
57   fgets(clogin, 200, stdin);
58   clogin[strlen(clogin) - 1] = '\0';
59   login = [NSString stringWithCString:clogin];
60   return login;
61 }
62
63 - (NSString *)promptPassword:(NSString *)_prompt {
64   NSString *pwd;
65   char     *cpwd;
66
67   cpwd = getpass("password: ");
68   pwd = [NSString stringWithCString:cpwd];
69   return pwd;
70 }
71
72 - (id)callFailed:(WOResponse *)_response {
73   if ([_response status] == 401) {
74     NSString *wwwauth;
75     NSString *user;
76     NSString *pass;
77     
78     wwwauth = [_response headerForKey:@"www-authenticate"];
79     if ([[wwwauth lowercaseString] hasPrefix:@"digest"])
80       [self logWithFormat:@"Digest authentication:\n'%@'", wwwauth];
81
82     // TODO: test credentials of URL
83     
84     if (self->defLogin) {
85       user = [self->defLogin autorelease];
86       self->defLogin = nil;
87     }
88     else
89       user = [self prompt:@"login:    "];
90     
91     if (self->defPassword) {
92       pass = [self->defPassword autorelease];
93       self->defPassword = nil;
94     }
95     else
96       pass = [self promptPassword:@"password: "];
97     
98     [self setUserName:user];
99     [self setPassword:pass];
100     
101     /* this "should" return some kind of "need-pwd" object ... */
102     return nil;
103   }
104   else {
105     return [super callFailed:_response];
106   }
107 }
108
109 @end /* HandleCredentialsClient */