<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Jakob Külzer &#187; Grails JSecurity Tutorial Guide</title>
	<atom:link href="http://www.jakusys.de/blog/tag/grails-jsecurity-tutorial-guide/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jakusys.de/blog</link>
	<description>Ninja Coding Monkey goes Canada</description>
	<lastBuildDate>Sun, 02 Jan 2011 20:12:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.3</generator>
		<item>
		<title>Custom Authentication Schemes with Grails and JSecurity</title>
		<link>http://www.jakusys.de/blog/2008/08/custom-authentication-schemes-with-grails-and-jsecurity/</link>
		<comments>http://www.jakusys.de/blog/2008/08/custom-authentication-schemes-with-grails-and-jsecurity/#comments</comments>
		<pubDate>Tue, 05 Aug 2008 19:47:56 +0000</pubDate>
		<dc:creator>Jakob Külzer</dc:creator>
				<category><![CDATA[Grails]]></category>
		<category><![CDATA[Grails JSecurity Tutorial Guide]]></category>

		<guid isPermaLink="false">http://www.jakusys.de/blog/?p=118</guid>
		<description><![CDATA[In my current software project a requirement is an authentication scheme consisting not of the usual user name an password, but user name, password and a store number. Each user name should be unique in for a store but could occur multiple times for all stores. For several reasons I decided to implement authentication with [...]]]></description>
			<content:encoded><![CDATA[<p>In my current software project a requirement is an authentication scheme consisting not of the usual user name an password, but user name, password and a store number. Each user name should be unique in for a store but could occur multiple times for all stores.</p>
<p>For several reasons I decided to implement authentication with <a title="JSecurity Plugin" href="http://www.jsecurity.org/releases/0.9.0-alpha/docs/api/org/jsecurity/authc/Account.html" target="_blank">JSecurity</a>. The Grails plugin is a great help and the quickstart brings you up to speed pretty fast.</p>
<p>As the <a title="Jsecurity Docs" href="http://www.jsecurity.org/docs" target="_blank">JSecurity documentation</a> is pretty scarce on the topic of custom authentication schemes, I'm posting my findings here in my blog. Perhaps it is useful to other developers as well (actually i would love to see some comments on this <img src='http://www.jakusys.de/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> .</p>
<p><span id="more-118"></span></p>
<p>You'll need a working grails application and the JSecurity plugin. You can install simply by typing <strong>grails install-plugin jsecurity</strong>. After that you should call <strong>grails quickstart</strong>, which is a script that creates among other a couple of domain classes and controllers.</p>
<p>Before we start some terms:</p>
<p><strong>AuthenticationToken</strong>: holds authentication information for an authentication request; this is the container that holds your username/password pair, your PGP key, whatever.</p>
<p><strong>Realm</strong>: acts as the part of the authentication system that decides whether a given AuthenticationToken is confirmed and granted access.</p>
<p>The trick for the custom authentication scheme is to create a custom <a title="AuthenticationToken" href="http://www.jsecurity.org/releases/0.9.0-alpha/docs/api/org/jsecurity/authc/AuthenticationToken.html" target="_blank">AuthenticationToken</a> and a customized <a title="Realm" href="http://www.jsecurity.org/releases/0.9.0-alpha/docs/api/org/jsecurity/realm/Realm.html" target="_blank">Realm</a>. A customized token could look like this (don't tell me its ugly code - its not meant to be a cut'n'paste solution; it's just to get the idea!):</p>
<pre>class CustomToken implements AuthenticationToken {
  String username
  String password
  Integer storeNumber

  public Object getPrincipal() {
    "${storeNumber}:${username}" // This could be done nicer, however i do not know how... yet <img src='http://www.jakusys.de/blog/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' />
  }
  public Object getCredentials() {
    password
  }
  // ... left out uninteressting methods.
}</pre>
<p>So much for the token. That was straight forward. Now to the realm which requires some more coding. Of course you can write you realm from scratch, implementing the interface. That, however, is quite a piece of work, so simple modified the <strong>JsecDbRealm</strong> created by the quickstart script (again, only the important parts):</p>
<pre>class JsecDbRealm {
  // We need this to tell JSecurity that this realm is responsible for our custom tokens:
  static authTokenClass = de.jakusys.example.CustomToken

  def authenticate(authToken) {
    def username = authToken.username
    // In addition to the username we need the store number for retrieval from the database:
    def storeNumber = authToken.storeNumber 

    // Null username is invalid
    if (username == null) {
      throw new AccountException('Null usernames are not allowed by this realm.')
    }
    // We need to check the format of the store number as well:
    try {
      storeNumber = Integer.parseInt(storeNumber)
    } catch (NumberFormatException ex) {
       throw new AccountException('Invalid store number.')
    }

    // Fetch the appropriate row from the database:
    def user = JsecUser.findByUsernameAndStoreNumber(username, storeNumber)
    if (!user) {
      throw new UnknownAccountException("No account found for user [${authToken.storeNumber}:${username}]")
    }

    // From here one you can re-use the code generated by the quickstart script...
  }

  // ... more uninteresting stuff ...
}</pre>
<p>This is a most basic implementation that uses the default <a title="SimpleAccount" href="http://www.jsecurity.org/releases/0.9.0-alpha/docs/api/org/jsecurity/authc/SimpleAccount.html" target="_blank">SimpleAccount</a> class which could be considered ugly, as it does not know anything about the store number. Depending on your needs you can create your own implementation of <a title="Account" href="http://www.jsecurity.org/releases/0.9.0-alpha/docs/api/org/jsecurity/authc/Account.html" target="_blank">Account</a> or stick to the pre-implemented variant (I still have to check wether this has any security implications).</p>
<p>With the CustomToken and the modified realm in place, the only thing to be done is modifying the login form and the AuthenticationController, but that's a piece of cake now. <img src='http://www.jakusys.de/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>I mope my notes are of any use to you. I so, leave a comment.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jakusys.de/blog/2008/08/custom-authentication-schemes-with-grails-and-jsecurity/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>

