Commits

Sebastian Sdorra  committed caff372

added code snippet 004 - authentication

  • Participants
  • Parent commits 5ca19f2

Comments (0)

Files changed (3)

File 004-authentication/pom.xml

+<?xml version="1.0"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+
+  <modelVersion>4.0.0</modelVersion>
+
+  <parent>
+    <artifactId>scm-plugins</artifactId>
+    <groupId>sonia.scm.plugins</groupId>
+    <version>1.8</version>
+  </parent>
+
+  <groupId>sonia.scm.snippets</groupId>
+  <artifactId>004-authentication</artifactId>
+  <version>1.0-SNAPSHOT</version>
+  <name>004-authentication</name>
+  <url>https://bitbucket.org/sdorra/scm-code-snippets</url>
+  <description>Add tab to repository configuration</description>
+  
+  <scm>
+    <connection>scm:hg:https://bitbucket.org/sdorra/scm-code-snippets</connection>
+    <developerConnection>scm:hg:https://bitbucket.org/sdorra/scm-code-snippets</developerConnection>
+    <url>https://bitbucket.org/sdorra/scm-code-snippets</url>
+  </scm>
+
+  <dependencies>
+
+    <dependency>
+      <groupId>javax.servlet</groupId>
+      <artifactId>servlet-api</artifactId>
+      <version>${servlet.version}</version>
+      <scope>provided</scope>
+    </dependency>
+
+  </dependencies>
+  
+  <repositories>
+    
+    <repository>
+      <id>maven.scm-manager.org</id>
+      <name>scm-manager release repository</name>
+      <url>http://maven.scm-manager.org/nexus/content/groups/public</url>
+    </repository>
+  
+  </repositories>
+
+</project>

File 004-authentication/src/main/java/sonia/scm/snippets/SnippetAuthentication.java

+/**
+ * Copyright (c) 2010, Sebastian Sdorra All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer. 2. Redistributions in
+ * binary form must reproduce the above copyright notice, this list of
+ * conditions and the following disclaimer in the documentation and/or other
+ * materials provided with the distribution. 3. Neither the name of SCM-Manager;
+ * nor the names of its contributors may be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * http://bitbucket.org/sdorra/scm-manager
+ *
+ */
+
+
+
+package sonia.scm.snippets;
+
+//~--- non-JDK imports --------------------------------------------------------
+
+import sonia.scm.SCMContextProvider;
+import sonia.scm.plugin.ext.Extension;
+import sonia.scm.user.User;
+import sonia.scm.web.security.AuthenticationHandler;
+import sonia.scm.web.security.AuthenticationResult;
+import sonia.scm.web.security.AuthenticationState;
+
+//~--- JDK imports ------------------------------------------------------------
+
+import java.io.IOException;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+/**
+ * Simple {@link AuthenticationHandler} example.
+ *
+ * @author Sebastian Sdorra
+ */
+@Extension
+public class SnippetAuthentication implements AuthenticationHandler
+{
+
+  /** Type of the {@link AuthenticationHandler} */
+  public static final String TYPE = "snippet";
+
+  //~--- methods --------------------------------------------------------------
+
+  /**
+   * Authenticate the user by username and password.
+   *
+   *
+   * @param request current http request
+   * @param response current http response
+   * @param username username of the user
+   * @param password password of the password
+   *
+   * @return
+   */
+  @Override
+  public AuthenticationResult authenticate(HttpServletRequest request,
+          HttpServletResponse response, String username, String password)
+  {
+    AuthenticationResult result = AuthenticationResult.NOT_FOUND;
+
+    if ("scm".equals(username))
+    {
+      // user found
+      if ("scmpwd".equals(password))
+      {
+        // authentication success
+        // create a user object
+        User user = new User(username);
+
+        // create the authentication result
+        result = new AuthenticationResult(user, AuthenticationState.SUCCESS);
+      }
+      else
+      {
+        // user found but authentication failed
+        result = AuthenticationResult.FAILED;
+      }
+    }
+
+    return result;
+  }
+
+  /**
+   * Close database connections or something else.
+   *
+   *
+   * @throws IOException
+   */
+  @Override
+  public void close() throws IOException
+  {
+
+    // do nothing
+  }
+
+  /**
+   * Init database connections or something else.
+   *
+   *
+   * @param context the context of the current SCM-Manager instance.
+   */
+  @Override
+  public void init(SCMContextProvider context)
+  {
+
+    // do nothing
+  }
+
+  //~--- get methods ----------------------------------------------------------
+
+  /**
+   * Returns the type of the {@link AuthenticationHandler}
+   *
+   *
+   * @return type of the {@link AuthenticationHandler}
+   */
+  @Override
+  public String getType()
+  {
+    return TYPE;
+  }
+}

File 004-authentication/src/main/resources/META-INF/scm/plugin.xml

+<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+<!--
+
+    Copyright (c) 2010, Sebastian Sdorra
+    All rights reserved.
+
+    Redistribution and use in source and binary forms, with or without
+    modification, are permitted provided that the following conditions are met:
+
+    1. Redistributions of source code must retain the above copyright notice,
+       this list of conditions and the following disclaimer.
+    2. Redistributions in binary form must reproduce the above copyright notice,
+       this list of conditions and the following disclaimer in the documentation
+       and/or other materials provided with the distribution.
+    3. Neither the name of SCM-Manager; nor the names of its
+       contributors may be used to endorse or promote products derived from this
+       software without specific prior written permission.
+
+    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+    AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+    DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
+    DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+    (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+    ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+    http://bitbucket.org/sdorra/scm-manager
+
+
+-->
+
+<plugin>
+
+  <information>
+    <author>Sebastian Sdorra</author>
+    <category>Code-Snippet</category>
+  </information>
+
+  <conditions>
+    <min-version>${project.parent.version}</min-version>
+  </conditions>
+  
+  <packages>
+    <package>sonia.scm.snippets</package>
+  </packages>
+  
+</plugin>