001 /* Copyright 2006-2009 the original author or authors. 002 * 003 * Licensed under the Apache License, Version 2.0 (the "License"); 004 * you may not use this file except in compliance with the License. 005 * You may obtain a copy of the License at 006 * 007 * http://www.apache.org/licenses/LICENSE-2.0 008 * 009 * Unless required by applicable law or agreed to in writing, software 010 * distributed under the License is distributed on an "AS IS" BASIS, 011 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 012 * See the License for the specific language governing permissions and 013 * limitations under the License. 014 */ 015 package org.codehaus.groovy.grails.plugins.springsecurity.facebook; 016 017 import org.springframework.security.GrantedAuthority; 018 import org.springframework.security.providers.AbstractAuthenticationToken; 019 020 /** 021 * Authentication token with Facebook-specific extra information. 022 * 023 * @author <a href='mailto:beckwithb@studentsonly.com'>Burt Beckwith</a> 024 */ 025 public class FacebookAuthenticationToken extends AbstractAuthenticationToken { 026 027 private static final long serialVersionUID = 1022970403466610153L; 028 029 private Status _status; 030 private long _userId; 031 private String _sessionKey; 032 private String _errorMessage; 033 034 /** 035 * Token type. 036 */ 037 public static enum Status { 038 /** successful authentication. */ 039 success, 040 /** failed authentication. */ 041 failure, 042 /** authentication error. */ 043 error 044 } 045 046 /** 047 * Created by the OpenIDAuthenticationProvider on successful authentication. 048 * @param authorities roles 049 * @param userId 050 * @param sessionKey 051 */ 052 public FacebookAuthenticationToken(final GrantedAuthority[] authorities, 053 final long userId, final String sessionKey) { 054 super(authorities); 055 _status = Status.success; 056 _userId = userId; 057 _sessionKey = sessionKey; 058 setAuthenticated(true); 059 } 060 061 /** 062 * Created by {@link FacebookAuthenticationProcessingFilter} from Facebook login info, 063 * but before loading roles. 064 * @param userId the UID 065 * @param sessionKey the session key 066 */ 067 public FacebookAuthenticationToken(final long userId, final String sessionKey) { 068 super(new GrantedAuthority[0]); 069 _status = Status.success; 070 _userId = userId; 071 _sessionKey = sessionKey; 072 setAuthenticated(false); 073 } 074 075 /** 076 * Create a failure token. 077 * @param status a non-success token 078 * @param errorMessage the error message 079 */ 080 public FacebookAuthenticationToken(final Status status, final String errorMessage) { 081 super(new GrantedAuthority[0]); 082 _status = status; 083 _errorMessage = errorMessage; 084 setAuthenticated(false); 085 } 086 087 /** 088 * {@inheritDoc} 089 * @see org.springframework.security.providers.AbstractAuthenticationToken#getCredentials() 090 */ 091 public Object getCredentials() { 092 // we don't have access to password 093 return null; 094 } 095 096 /** 097 * {@inheritDoc} 098 * @see org.springframework.security.providers.AbstractAuthenticationToken#getPrincipal() 099 */ 100 public Object getPrincipal() { 101 return _userId; 102 } 103 104 /** 105 * The Facebook UID. 106 * @return the uid 107 */ 108 public long getUserId() { 109 return _userId; 110 } 111 112 /** 113 * The status. 114 * @return the status 115 */ 116 public Status getStatus() { 117 return _status; 118 } 119 120 /** 121 * The login session key. 122 * @return the key 123 */ 124 public String getSessionKey() { 125 return _sessionKey; 126 } 127 128 /** 129 * Get the error message (if status is <code>error</code>). 130 * @return the message 131 */ 132 public String getErrorMessage() { 133 return _errorMessage; 134 } 135 }