Switch User Access Forbidden - grails

I am trying to use the Switch User feature of grails using the documentation
application.groovy changes:
grails.plugin.springsecurity.controllerAnnotations.staticRules = [
[pattern: '/j_spring_security_switch_user', access: ['ROLE_TENANT_ADMIN']],
[pattern: '/j_spring_security_exit_user', access: ['permitAll']]
]
grails.plugin.springsecurity.switchUser.targetUrl = "/public/landing"
grails.plugin.springsecurity.useSwitchUserFilter = true
Also overridden the SwitchUser Filter since my role in the application is different from the ROLE_SWITCH_USER using the link
But whenever I click on the Switch form I am facing 403 Forbidden
<sec:ifAllGranted roles="ROLE_TENANT_ADMIN">
<form action="/j_spring_security_switch_user" method="POST">
Switch to user: <input name="j_username" type="text"
value="###email####">
<input type="submit" value="Switch"> </form>
</sec:ifAllGranted>

Related

Why is the request.protocol always http instead of https?

I have this page that does a POST request:
<form method="POST" action="https://staging.cathytest.com/user/signup">
<input name="data" value="tsd213guHUUDUhyusgd4314yDTSgts435dfsg" />
<input type="submit" value="Submit" />
</form>
Then the controller action signup will check if the request protocol is https. I get the protocol using request.protocol, but it always gives me http. Can you tell me what the problem is?
# config/application.rb
module MyApp
class Application < Rails::Application
config.force_ssl = true
end
end
This will force rails to use https for all requests.

Where do the S3 Policy and Secret Key go?

I'm new to AWS/S3 and following this tutorial from Amazon.
It's not working, and I'm wondering if it's due to the policy section.
Where does the policy go? For what it's worth, the form is in a React component. I tried putting the one below into the S3 policy in my AWS console, but it didn't like it.
I see the Access Key goes in the form. Where does the Secret key go? (Rails+React app)
Thanks for any guidance.
Here's the policy:
{ "expiration": "2007-12-01T12:00:00.000Z",
"conditions": [
{"bucket": "johnsmith"},
["starts-with", "$key", "user/eric/"],
{"acl": "public-read"},
{"success_action_redirect": "http://johnsmith.s3.amazonaws.com/successful_upload.html"},
["starts-with", "$Content-Type", "image/"],
{"x-amz-meta-uuid": "14365123651274"},
["starts-with", "$x-amz-meta-tag", ""]
]
}
And the form:
<form action="http://johnsmith.s3.amazonaws.com/" method="post" enctype="multipart/form-data">
Key to upload: <input type="input" name="key" value="user/eric/" /><br />
<input type="hidden" name="acl" value="public-read" />
<input type="hidden" name="success_action_redirect" value="http://johnsmith.s3.amazonaws.com/successful_upload.html" />
Content-Type: <input type="input" name="Content-Type" value="image/jpeg" /><br />
<input type="hidden" name="x-amz-meta-uuid" value="14365123651274" />
Tags for File: <input type="input" name="x-amz-meta-tag" value="" /><br />
<input type="hidden" name="AWSAccessKeyId" value="AKIAIOSFODNN7EXAMPLE" />
<input type="hidden" name="Policy" value="POLICY" />
<input type="hidden" name="Signature" value="SIGNATURE" />
File: <input type="file" name="file" /> <br />
<!-- The elements after this will be ignored -->
<input type="submit" name="submit" value="Upload to Amazon S3" />
</form>
...
The Policy value should be a base64-encoded version of your policy.
The Signature should be a hash based on the Policy and the Secret Key.
For example, this Python code (sorry, I don't have equivalent Ruby code handy) will output the two values for you:
#!/usr/bin/python
import base64
import hmac, hashlib
policy_document = '{ "expiration": "2007-12-01T12:00:00.000Z",
"conditions": [
{"bucket": "johnsmith"},
["starts-with", "$key", "user/eric/"],
{"acl": "public-read"},
{"success_action_redirect": "http://johnsmith.s3.amazonaws.com/successful_upload.html"},
["starts-with", "$Content-Type", "image/"],
{"x-amz-meta-uuid": "14365123651274"},
["starts-with", "$x-amz-meta-tag", ""]
]
}'
AWS_SECRET_ACCESS_KEY = "<SECRET-KEY>"
policy = base64.b64encode(policy_document)
signature = base64.b64encode(hmac.new(AWS_SECRET_ACCESS_KEY, policy, hashlib.sha1).digest())
print policy
print
print signature
Everything in your form looks alright, except POLICY & SIGNATURE:
Policy will be encoded as Base64 which includes some basic info about bucket, expiration, acl, etc,..
Signature will be encoded as Base64 as well which includes secret key, and above policy
Here is my example which describes how to generate POLICY & SIGNATURE:
class S3Upload
attr_reader :policy_document, :signature
def initialize
#policy_document = generate_policy_document
#signature = generate_signature
end
private
def generate_policy_document
Base64.encode64(
{
expiration: 1.day.from_now.utc.strftime('%Y-%m-%dT%H:%M:%S.000Z'),
conditions: [
{ bucket: YOUR_BUCKET },
{ acl: 'public-read' },
["starts-with", "$key", ""],
{ success_action_status: '201' },
["starts-with","$content-type", "image/"]
]
}.to_json
).gsub(/\n|\r/, '')
end
def generate_signature
Base64.encode64(
OpenSSL::HMAC.digest(
OpenSSL::Digest::Digest.new('sha1'),
YOUR_AWS_SECRET_ACCESS_KEY,
policy_document
)
).gsub(/\n/, '')
end
end
You can use this class to export policy & signature, then include to your form.
We don't include secret key to form directly, we use it to generate signature as my example above

Using Rails to automatically enter username and password on a third party site

I have access to an ASP.Net site, secured behind a simple username and password authentication, that I'd like my rails app to access and display the contents.
How do I tell my rails app enter the Username, Password and then Submit?
The input elements in question are shown below...
Username:
<input name="ctl00$ContentPlaceHolder1$txtUserName" type="text" id="ctl00_ContentPlaceHolder1_txtUserName">
Password:
<input name="ctl00$ContentPlaceHolder1$txtPassword" type="password" id="ctl00_ContentPlaceHolder1_txtPassword">
Submit:
<input type="submit" name="ctl00$ContentPlaceHolder1$btnLogIn" value="Log In" onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$ContentPlaceHolder1$btnLogIn", "", true, "", "", false, false))" id="ctl00_ContentPlaceHolder1_btnLogIn">

j_spring_security_check redirect

I upgraded my application from 2.2.3 to 2.4.3. Now when I click submit on the login page, I see that the POST request to j_spring_security_check has status code 302.
Is there something I need to do after upgrading? I've tried running grails clean-all but that did not help.
I'm using this version: compile ':spring-security-core:2.0-RC3'
settings in config.groovy
grails.plugins.springsecurity.userLookup.userDomainClassName = 'com.aerstone.scanner.security.User'
grails.plugins.springsecurity.userLookup.authorityJoinClassName = 'com.aerstone.scanner.security.UserRole'
grails.plugins.springsecurity.authority.className = 'com.aerstone.scanner.security.Role'
GSP
<form action="${postUrl}" method="POST" id="loginForm" autocomplete="off">
<input type='text' name='j_username' id='username' placeholder="Username"/>
<input type='password' name='j_password' id='password' placeholder="Password"/>
<input type="submit" class="btn" value="Sign in" id="submit"/>
</form>
I had the same issue after an upgrade.
I changed the action of the login form by:
<form action='${ request.contextPath }/j_spring_security_check' method="POST">
Hope that helps
please note that the name had change in the config to be:
grails.plugin.springsecurity....
Notice 'plugin' vs 'plugins'. I've passed this problem but I'm getting invalid credentials all the time.

phpCAS::isAuthenticated returning false after login in different service

I've configured the CAS login page so that it accepts the username and password as GET parameters and if these are provided, it submits the login form automatically. This looks as the CAS login page is not even touched during authentication from the website.
The website is using phpCAS version 1.3.2 to communicate with CAS. If I log in directly through the form, It works as expected: the browser gets redirected back from CAS correctly and isAuthenticated() returns true. However if I log in to a different service beforehand, isAuthenticated() returns false. If I'm not mistaken this is because I have to do an actual check with CAS if the auth is fine, therefore I've also tried checkAuthentication(), but I get the following errors:
[error] [client 192.168.12.120] PHP Fatal error: Uncaught exception 'CAS_AuthenticationException' in /home/dev/www/CAS-1.3.2/CAS/Client.php:2248
Stack trace:
#0 /home/dev/www/CAS-1.3.2/CAS/Client.php(1227): CAS_Client->_validatePGT('https://192.168...', '????<cas:servic...', Object(DOMElement))
#1 /home/dev/www/CAS-1.3.2/CAS/Client.php(1131): CAS_Client->isAuthenticated()
#2 /home/dev/www/CAS-1.3.2/CAS.php(1078): CAS_Client->checkAuthentication()
#3 /home/dev/www/redir.php(39): phpCAS::checkAuthentication()
#4 {main}
thrown in /home/dev/www/CAS-1.3.2/CAS/Client.php on line 2248, referer: https://192.168.10.144:8181/cas/login?username=myUser&password=testpassword&auto=true&service=https%3A%2F%2F192.168.12.120%2Fredir.php
CAS server log
phpCAS debug log
PHP Code:
<?php
function pageURL() {
$PROTOCOL = "http";
if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') {
$PROTOCOL = "https";
}
$url = "$PROTOCOL://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$url = strtok($url, '?');
return $url;
}
// Configuration
$phpcas_path = "CAS-1.3.2";
$cas_host = "192.168.10.144";
$cas_port = 8181;
$cas_context = "/cas";
// Load the CAS lib
require_once $phpcas_path . "/CAS.php";
// Enable debugging
phpCAS::setDebug ();
// Initialize phpCAS
phpCAS::proxy ( CAS_VERSION_2_0, $cas_host, $cas_port, $cas_context );
phpCAS::setNoCasServerValidation ();
// check CAS authentication
$auth = phpCAS::checkAuthentication();
// logout if desired
if (isset ( $_REQUEST ['logout'] )) {
phpCAS::logout ();
}
$serviceUrl = "https://192.168.10.144:8181/accessrights/";
?>
<html>
<head>
<title>CAS form login</title>
</head>
<body>
<?php if ($auth) {
phpCAS::serviceWeb($serviceUrl, $err_code, $accessrights);
?>
<p>Hello <strong><?php echo phpCAS::getUser(); ?></strong>! You have been authenticated with CAS. Your access rights are: <?php echo $accessrights; ?></p>
<?php } else { ?>
<h3>User login</h3>
<div>Enter your username and password here in order to log in on the website:</div>
<!-- ###LOGIN_FORM### -->
<form method="GET" action="https://192.168.10.144:8181/cas/">
<p>Username : <input type="text" name="username" /></p>
<p>Password : <input type="password" name="password" /></p>
<p>Remember me : <input type="checkbox" name="rememberMe" value="true" /></p>
<p><input type="submit" value="Login !" /></p>
<input type="hidden" name="auto" value="true" />
<input type="hidden" name="service" value="<?php echo pageURL(); ?>" />
</form>
<!-- ###LOGIN_FORM### -->
<?php } ?>
</body>
</html>
I see that checkAuthentication() fails to retrieve a Proxy Granting Ticket, but I have no clue about the reason. Any ideas? I also have a page with forceAuthentication(), and it works flawlessly.
According to your server's log, CAS is trying to callback your application via "https://xxx.xx.xx.xx/redir.php" but his encounter an SSL issue "TicketCreationException: error.authentication.credentials.bad"

Resources