<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Basic Authentication and Grails</title>
	<atom:link href="http://awalkingcity.com/blog/2008/03/07/basic-authentication-and-grails/feed/" rel="self" type="application/rss+xml" />
	<link>http://awalkingcity.com/blog/2008/03/07/basic-authentication-and-grails/</link>
	<description></description>
	<lastBuildDate>Wed, 21 Dec 2011 12:24:53 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.2</generator>
	<item>
		<title>By: Sebastian</title>
		<link>http://awalkingcity.com/blog/2008/03/07/basic-authentication-and-grails/comment-page-1/#comment-498</link>
		<dc:creator>Sebastian</dc:creator>
		<pubDate>Fri, 25 Mar 2011 13:56:02 +0000</pubDate>
		<guid isPermaLink="false">http://awalkingcity.com/blog/?p=12#comment-498</guid>
		<description>Thanks for the tip. One small bug in Romains code:

&lt;code&gt;
if(!ValidationUtil.validate(request,response, log, grailsApplication)){
render(text: &quot;User not known.&quot;)
return
}
}
&lt;/code&gt;

must read

&lt;code&gt;
if(!ValidationUtil.validate(request,response, log, grailsApplication)){
render(text: &quot;User not known.&quot;)
return &lt;em&gt;false&lt;/em&gt;
}
}
&lt;/code&gt;

in order to prevent execution of the action if the user is not logged in.</description>
		<content:encoded><![CDATA[<p>Thanks for the tip. One small bug in Romains code:</p>
<p><code><br />
if(!ValidationUtil.validate(request,response, log, grailsApplication)){<br />
render(text: "User not known.")<br />
return<br />
}<br />
}<br />
</code></p>
<p>must read</p>
<p><code><br />
if(!ValidationUtil.validate(request,response, log, grailsApplication)){<br />
render(text: "User not known.")<br />
return <em>false</em><br />
}<br />
}<br />
</code></p>
<p>in order to prevent execution of the action if the user is not logged in.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Romain</title>
		<link>http://awalkingcity.com/blog/2008/03/07/basic-authentication-and-grails/comment-page-1/#comment-495</link>
		<dc:creator>Romain</dc:creator>
		<pubDate>Thu, 24 Feb 2011 10:31:19 +0000</pubDate>
		<guid isPermaLink="false">http://awalkingcity.com/blog/?p=12#comment-495</guid>
		<description>I changed you code in order to log with a login and password in the configuration file.

I also add the WWW-Authenticate in order to respect the standard : 
&lt;code&gt;
  response.addHeader(&quot;WWW-Authenticate&quot;, &quot;Basic realm=\&quot;Secure Area\&quot;&quot;)
&lt;/code&gt;
&lt;code&gt;
class SecurityFilters {
    def grailsApplication
    def filters = {
        basicAuth(controller: &#039;prospect&#039;, action: &#039;*&#039;) {
            before = {
                if(!ValidationUtil.validate(request,response, log, grailsApplication)){
                  render(text: &quot;User not known.&quot;)
                  return
                }
            }
        }

        basicAuth(controller: &#039;hash&#039;, action: &#039;*&#039;) {
            before = {
                if(!ValidationUtil.validate(request,response, log, grailsApplication)){
                  render(text: &quot;User not known.&quot;)
                  return
                }
            }
        }

    }
}

/**
 * Basic Authentication
 * return false if login failed, and true if login is successful
 *
 * The HTTP status code and the HTTP header of the response are set as this :
 *
 * &quot;Authorization&quot; not present in the header of the request :
 *  - status : 401
 *  - add hedaer : WWW-Authenticate: Basic realm=&quot;Secure Area&quot;
 *
 *  &quot;Authorization&quot; present, but bad login / password
 *  - status : 403
 *
 */
class ValidationUtil{
    static validate(def request, def response, def log, def grailsApplication){
            try {
                def authString = request.getHeader(&#039;Authorization&#039;)
                if (!authString) {
                    response.status = 401
                    response.addHeader(&quot;WWW-Authenticate&quot;, &quot;Basic realm=\&quot;Secure Area\&quot;&quot;)
                    return false
                }
                def encodedPair = authString - &#039;Basic &#039;
                def decodedPair = new String(new sun.misc.BASE64Decoder().decodeBuffer(encodedPair));
                def credentials = decodedPair.split(&#039;:&#039;)
                def login = grailsApplication.config.admin.security.login
                def password = grailsApplication.config.admin.security.password

                if (login.equals(credentials[0]) &amp;&amp; password.equals(credentials[1])) {
                    return true
                } else {
                    log.warn(&quot;User not known&quot;)
                    response.status = 403
                    return false
                }
            } catch (Exception e) {
                log.warn(&quot;User not known&quot;, e)
                response.status = 403
                return false
            }
        }
}
&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>I changed you code in order to log with a login and password in the configuration file.</p>
<p>I also add the WWW-Authenticate in order to respect the standard :<br />
<code><br />
  response.addHeader("WWW-Authenticate", "Basic realm=\"Secure Area\"")<br />
</code><br />
<code><br />
class SecurityFilters {<br />
    def grailsApplication<br />
    def filters = {<br />
        basicAuth(controller: 'prospect', action: '*') {<br />
            before = {<br />
                if(!ValidationUtil.validate(request,response, log, grailsApplication)){<br />
                  render(text: "User not known.")<br />
                  return<br />
                }<br />
            }<br />
        }</p>
<p>        basicAuth(controller: 'hash', action: '*') {<br />
            before = {<br />
                if(!ValidationUtil.validate(request,response, log, grailsApplication)){<br />
                  render(text: "User not known.")<br />
                  return<br />
                }<br />
            }<br />
        }</p>
<p>    }<br />
}</p>
<p>/**<br />
 * Basic Authentication<br />
 * return false if login failed, and true if login is successful<br />
 *<br />
 * The HTTP status code and the HTTP header of the response are set as this :<br />
 *<br />
 * "Authorization" not present in the header of the request :<br />
 *  - status : 401<br />
 *  - add hedaer : WWW-Authenticate: Basic realm="Secure Area"<br />
 *<br />
 *  "Authorization" present, but bad login / password<br />
 *  - status : 403<br />
 *<br />
 */<br />
class ValidationUtil{<br />
    static validate(def request, def response, def log, def grailsApplication){<br />
            try {<br />
                def authString = request.getHeader('Authorization')<br />
                if (!authString) {<br />
                    response.status = 401<br />
                    response.addHeader("WWW-Authenticate", "Basic realm=\"Secure Area\"")<br />
                    return false<br />
                }<br />
                def encodedPair = authString - 'Basic '<br />
                def decodedPair = new String(new sun.misc.BASE64Decoder().decodeBuffer(encodedPair));<br />
                def credentials = decodedPair.split(':')<br />
                def login = grailsApplication.config.admin.security.login<br />
                def password = grailsApplication.config.admin.security.password</p>
<p>                if (login.equals(credentials[0]) &amp;&amp; password.equals(credentials[1])) {<br />
                    return true<br />
                } else {<br />
                    log.warn("User not known")<br />
                    response.status = 403<br />
                    return false<br />
                }<br />
            } catch (Exception e) {<br />
                log.warn("User not known", e)<br />
                response.status = 403<br />
                return false<br />
            }<br />
        }<br />
}<br />
</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Building Blocks &#187; Grails: Ajax Login using Basic Auth</title>
		<link>http://awalkingcity.com/blog/2008/03/07/basic-authentication-and-grails/comment-page-1/#comment-246</link>
		<dc:creator>Building Blocks &#187; Grails: Ajax Login using Basic Auth</dc:creator>
		<pubDate>Tue, 07 Apr 2009 19:07:08 +0000</pubDate>
		<guid isPermaLink="false">http://awalkingcity.com/blog/?p=12#comment-246</guid>
		<description>[...] you want. If you want a simple solution and stick with the HTTP standard you could use Basic auth. Here is a good explaination of how to implement Basic auth for Grails. Another way is too use a cookie to hold authorization [...]</description>
		<content:encoded><![CDATA[<p>[...] you want. If you want a simple solution and stick with the HTTP standard you could use Basic auth. Here is a good explaination of how to implement Basic auth for Grails. Another way is too use a cookie to hold authorization [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: a walking city &#187; Blog Archive &#187; OpenID and Grails</title>
		<link>http://awalkingcity.com/blog/2008/03/07/basic-authentication-and-grails/comment-page-1/#comment-8</link>
		<dc:creator>a walking city &#187; Blog Archive &#187; OpenID and Grails</dc:creator>
		<pubDate>Thu, 20 Mar 2008 23:51:17 +0000</pubDate>
		<guid isPermaLink="false">http://awalkingcity.com/blog/?p=12#comment-8</guid>
		<description>[...] Our final step is to set up an Authentication filter like the one we made for basic authentication . The only difference is that we have a number of controller actions that we want to allow to pass [...]</description>
		<content:encoded><![CDATA[<p>[...] Our final step is to set up an Authentication filter like the one we made for basic authentication . The only difference is that we have a number of controller actions that we want to allow to pass [...]</p>
]]></content:encoded>
	</item>
</channel>
</rss>

