gpt4 book ai didi

objective-c - 如何使用 NSURLConnection 发布 SAML 响应消息?

转载 作者:太空狗 更新时间:2023-10-30 03:45:58 26 4
gpt4 key购买 nike

我正在创建一个允许用户与安全服务器交互的 iPhone 应用程序。但是,要对用户进行身份验证,应用程序需要从用户登录到数据所在的服务器(提供 SSO)的服务器通过 SAML。因此,该应用程序正在模仿发送 SAML HTTP 请求(通常由浏览器处理)。

该过程正在使用 Mechanize gem 与 Ruby 一起工作,但现在我无法使用 NSURLConnection 让它在 iPhone 上工作。

我遇到的问题是:NSURLConnection 似乎没有将 base64 编码的 SAML 响应正确发送到服务器。 + 号被空格替换,导致服务器端对 SAML 响应的解码失败。将 URL 编码与 stringByAddingPercentEscapesUsingEncoding 一起使用会导致相同的解码错误。

这是提取和发送 SAML 响应的代码:

// Extract the SAML Response from the form that is sent by the server
body = [[NSString alloc] initWithData:[self receivedData] encoding:NSUTF8StringEncoding];
NSString *searchSAMLResponseStart = @"name=\"SAMLResponse\" value=\"";
NSString *searchSAMLResponseEnd = @">\n<NOSCRIPT><INPUT TYPE=\"SUBMIT\"";
NSRange samlResponseStartRange = [body rangeOfString:searchSAMLResponseStart options:0];
NSRange samlResponseEndRange = [body rangeOfString:searchSAMLResponseEnd options:0];
NSRange range = NSMakeRange (NSMaxRange(samlResponseStartRange), samlResponseEndRange.location - NSMaxRange(samlResponseStartRange) -1);
NSString* samlResponseString = [body substringWithRange:range];

// Construct the SAML POST request
NSURL *url;
NSMutableURLRequest *request;
url = [NSURL URLWithString:kSamlPostUrl];
request = [NSMutableURLRequest requestWithURL:url];
NSString *userAgent = kUserAgent;
[request setValue:userAgent forHTTPHeaderField:@"User-Agent"];
[request setHTTPMethod:@"POST"];
NSString *post = [@"RelayState=https://example.com&SAMLResponse=" stringByAppendingString:samlResponseString];
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];

// Send the request
self.connectionPostSaml = [NSURLConnection connectionWithRequest:request delegate:self];

服务器发送的 SAML 表单和 SAML 响应的来源,如下所示:

<html>
<HEAD><META HTTP-EQUIV='PRAGMA' CONTENT='NO-CACHE'><META HTTP-EQUIV='CACHE-CONTROL' CONTENT='NO-CACHE'><TITLE>SAML 2.0 Auto-POST form</TITLE></HEAD>
<body onLoad="document.forms[0].submit()">
<NOSCRIPT>Your browser does not support JavaScript. Please click the 'Continue' button below to proceed. <br><br></NOSCRIPT>
<form action="https://sso.example.com/saml2" method="POST">
<input type="hidden" name="RelayState" value="https://example.com">
<input type="hidden" name="SAMLResponse" value="PFJlc3BvbnNlIHhtbG5zPSJ1cm46b2FzaXM6bmFtZXM6dGM6U0FNTDoyLjA6cHJvdG9jb2wiIERl
c3RpbmF0aW9uPSJodHRwczovL3Nzby5vcG93ZXIuY29tL3NwL0FDUy5zYW1sMiIgSUQ9Il85ZWY0
M2MzMGRkZmQ0YzY1ODNiMjgxZjAwNDU5ZWQyMjZmMjQiIElzc3VlSW5zdGFudD0iMjAxMi0wMy0y
OFQyMTo0MDowNloiIFZlcnNpb249IjIuMCI+CiAgICA8bnMxOklzc3VlciB4bWxuczpuczE9InVy
bjpvYXNpczpuYW1lczp0YzpTQU1MOjIuMDphc3NlcnRpb24iIEZvcm1hdD0idXJuOm9hc2lzOm5h
bWVzOnRjOlNBTUw6Mi4wOm5hbWVpZC1mb3JtYXQ6ZW50aXR5Ij5wZ2UuY29tPC9uczE6SXNzdWVy
(…)
cnRpb24+CjwvUmVzcG9uc2U+">
<NOSCRIPT><INPUT TYPE="SUBMIT" VALUE="Continue"></NOSCRIPT>
</form>
</body>
</html>

以及服务端收到POST请求后产生的错误:

Unable to parse incoming SAML2 message, raw:     PFJlc3BvbnNlIHhtbG5zPSJ1cm46b2FzaXM6bmFtZXM6dGM6U0FNTDoyLjA6cHJvdG9jb2wiIERl
c3RpbmF0aW9uPSJodHRwczovL3Nzby5vcG93ZXIuY29tL3NwL0FDUy5zYW1sMiIgSUQ9Il9iN2Q5
OTZhMmI1MjhiNWRkNjY0YWM4YjUwZmVjM2M2OTMzMWEiIElzc3VlSW5zdGFudD0iMjAxMi0wMy0y
OVQyMjowMzoxNVoiIFZlcnNpb249IjIuMCI CiAgICA8bnMxOklzc3VlciB4bWxuczpuczE9InVy
bjpvYXNpczpuYW1lczp0YzpTQU1MOjIuMDphc3NlcnRpb24iIEZvcm1hdD0idXJuOm9hc2lzOm5h
bWVzOnRjOlNBTUw6Mi4wOm5hbWVpZC1mb3JtYXQ6ZW50aXR5Ij5wZ2UuY29tPC9uczE6SXNzdWVy
PgogICAgPFN0YXR1cz4KICAgICAgICA8U3RhdHVzQ29kZSBWYWx1ZT0idXJuOm9hc2lzOm5hbWVz
OnRjOlNBTUw6Mi4wOnN0YXR1czpTdWNjZXNzIi8 CiAgICA8L1N0YXR1cz4KICAgIDxuczI6QXNz
(…)

注意原始响应中的 + 号是如何变成空格的。

服务器解码接收到的响应并因此用空格替换 + 符号似乎是正常行为,但是:为什么当 SAML 表单从浏览器而不是从 NSURLConnection 发布时它可以工作?

为了解决我试过:

  • 使用不同的编码对 SAML 响应进行解码和编码。现在使用NSUTF8StringEncoding,也尝试过:NSASCIIStringEncoding、NSNEXTSTEPStringEncoding、NSNonLossyASCIIStringEncoding、NSUnicodeStringEncoding

  • 使用 stringByAddingPercentEscapesUsingEncoding,所以这行代码:

    NSString* samlResponseString = [body substringWithRange:range];

变成:

NSString* samlResponseString = [[body substringWithRange:range] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  • 为表单使用其他 enctypes,但服务器甚至不接受这些请求:多部分/表单数据文本/纯

非常感谢您的帮助。提前致谢!

最佳答案

我在通过 post 发送 base64 时遇到问题,并通过 url 编码解决了它。试试这个:

NSString *post = [@"RelayState=https://example.com&SAMLResponse=" stringByAppendingString:
(__bridge_transfer NSString *)CFURLCreateStringByAddingPercentEscapes(NULL,
(__bridge CFStringRef)samlResponseString, NULL, (CFStringRef)@"!*'\"();:@&=+$,/?%#[]% ",
CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding))];

关于objective-c - 如何使用 NSURLConnection 发布 SAML 响应消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9934719/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com