@ -1,5 +1,7 @@
package org.gcube.portal.socialmail ;
import static org.gcube.common.authorization.client.Constants.authorizationService ;
import java.security.GeneralSecurityException ;
import java.util.ArrayList ;
import java.util.Date ;
@ -39,6 +41,7 @@ import org.gcube.common.homelibrary.home.workspace.exceptions.WorkspaceFolderNot
import org.gcube.common.homelibrary.home.workspace.sharing.WorkspaceMessage ;
import org.gcube.common.homelibrary.home.workspace.sharing.WorkspaceMessageManager ;
import org.gcube.common.portal.PortalContext ;
import org.gcube.common.scope.api.ScopeProvider ;
import org.gcube.portal.databook.server.DatabookStore ;
import org.gcube.portal.databook.shared.Comment ;
import org.gcube.portal.databook.shared.Feed ;
@ -94,6 +97,7 @@ public class PeriodicTask implements Runnable {
* @return a fake session usuful for Notifications
* /
private ASLSession getFakeASLSession ( String emailAddress ) {
ASLSession toReturn = null ;
String sessionID = UUID . randomUUID ( ) . toString ( ) ;
PortalContext context = PortalContext . getConfiguration ( ) ;
String scope = "/" + context . getInfrastructureName ( ) ;
@ -116,16 +120,31 @@ public class PeriodicTask implements Runnable {
SessionManager . getInstance ( ) . getASLSession ( sessionID , username ) . setUserGender ( isMale ? GenderType . MALE : GenderType . FEMALE ) ;
_log . debug ( "Created fakesession for user " + username + " email=" + emailAddress ) ;
_log . debug ( "Setting token for user " + username ) ;
toReturn = SessionManager . getInstance ( ) . getASLSession ( sessionID , username ) ;
setAuthorizationToken ( toReturn ) ;
} catch ( Exception e ) {
_log . error ( "Exception while trying to get the user from email address: " + e . getMessage ( ) ) ;
return null ;
}
return SessionManager . getInstance ( ) . getASLSession ( sessionID , username ) ;
return toReturn ;
}
private final static String DEFAULT_ROLE = "OrganizationMember" ;
private static void setAuthorizationToken ( ASLSession session ) {
String username = session . getUsername ( ) ;
String scope = session . getScope ( ) ;
ScopeProvider . instance . set ( scope ) ;
_log . debug ( "calling service token on scope " + scope ) ;
List < String > userRoles = new ArrayList < > ( ) ;
userRoles . add ( DEFAULT_ROLE ) ;
session . setSecurityToken ( null ) ;
String token = authorizationService ( ) . build ( ) . generate ( session . getUsername ( ) , userRoles ) ;
_log . debug ( "received token: " + token ) ;
session . setSecurityToken ( token ) ;
_log . info ( "Security token set in session for: " + username + " on " + scope ) ;
}
public void check ( String portalName , String host , String user , String password ) {
try {
@ -168,11 +187,12 @@ public class PeriodicTask implements Runnable {
_log . info ( "Parsing email of " + message . getFrom ( ) [ 0 ] + " with subject: " + subject ) ;
if ( isValidReply ( message ) ) {
String subAddressField = extractSubaddress ( message ) ;
_log . info ( "extractSubaddress filed = " + subAddressField ) ;
Address [ ] froms = message . getFrom ( ) ;
String emailAddress = froms = = null ? null : ( ( InternetAddress ) froms [ 0 ] ) . getAddress ( ) ;
ASLSession fakeSession = getFakeASLSession ( emailAddress ) ;
if ( fakeSession ! = null ) {
if ( fakeSession ! = null & & subAddressField ! = null ) {
if ( subAddressField . endsWith ( AppType . POST . toString ( ) ) | | subAddressField . endsWith ( AppType . POST . toString ( ) . toLowerCase ( ) ) ) { //it is a post, a comment on a post or a mention
_log . debug ( "Looks like a post, a comment on a post or a mention to me" ) ;
String feedId = extractIdentifier ( subAddressField ) ;
@ -447,12 +467,20 @@ public class PeriodicTask implements Runnable {
* /
private static String extractSubaddress ( Message message ) throws MessagingException {
Address [ ] emails = message . getRecipients ( RecipientType . TO ) ;
String toParse = emails [ 0 ] . toString ( ) ;
int plus = toParse . indexOf ( '+' ) ;
int at = toParse . indexOf ( '@' ) ;
if ( plus = = - 1 )
return null ;
return toParse . substring ( plus + 1 , at ) ;
String subAddressToReturn = null ;
for ( int i = 0 ; i < emails . length ; i + + ) {
String toParse = emails [ i ] . toString ( ) ;
int plus = toParse . indexOf ( '+' ) ;
int at = toParse . indexOf ( '@' ) ;
if ( plus > = 0 ) {
subAddressToReturn = toParse . substring ( plus + 1 , at ) ;
break ;
}
else {
_log . warn ( "Found recipient with no subaddress, skipping " + toParse ) ;
}
}
return subAddressToReturn ;
}
/ * *
* extracts the identifier ( the part before the $ ) from the Subaddress