| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
package org.xwiki.crypto.store.wiki.internal; |
| 21 |
|
|
| 22 |
|
import java.io.IOException; |
| 23 |
|
|
| 24 |
|
import javax.inject.Inject; |
| 25 |
|
import javax.inject.Named; |
| 26 |
|
import javax.inject.Provider; |
| 27 |
|
import javax.inject.Singleton; |
| 28 |
|
|
| 29 |
|
import org.xwiki.component.annotation.Component; |
| 30 |
|
import org.xwiki.crypto.BinaryStringEncoder; |
| 31 |
|
import org.xwiki.crypto.store.SignatureStore; |
| 32 |
|
import org.xwiki.crypto.store.SignatureStoreException; |
| 33 |
|
import org.xwiki.model.EntityType; |
| 34 |
|
import org.xwiki.model.reference.DocumentReference; |
| 35 |
|
import org.xwiki.model.reference.DocumentReferenceResolver; |
| 36 |
|
import org.xwiki.model.reference.EntityReference; |
| 37 |
|
import org.xwiki.model.reference.LocalDocumentReference; |
| 38 |
|
|
| 39 |
|
import com.xpn.xwiki.XWikiContext; |
| 40 |
|
import com.xpn.xwiki.XWikiException; |
| 41 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
| 42 |
|
import com.xpn.xwiki.objects.BaseObject; |
| 43 |
|
|
| 44 |
|
|
| 45 |
|
@link |
| 46 |
|
|
| 47 |
|
@version |
| 48 |
|
@since |
| 49 |
|
|
| 50 |
|
@Component |
| 51 |
|
@Singleton |
| |
|
| 81.8% |
Uncovered Elements: 8 (44) |
Complexity: 13 |
Complexity Density: 0.39 |
|
| 52 |
|
public class DefaultSignatureStore implements SignatureStore |
| 53 |
|
{ |
| 54 |
|
|
| 55 |
|
|
| 56 |
|
|
| 57 |
|
public static final LocalDocumentReference SIGNATURECLASS = new LocalDocumentReference("Crypto", "SignatureClass"); |
| 58 |
|
|
| 59 |
|
|
| 60 |
|
|
| 61 |
|
|
| 62 |
|
public static final String SIGNATURECLASS_PROP_REFERENCE = "reference"; |
| 63 |
|
|
| 64 |
|
|
| 65 |
|
|
| 66 |
|
|
| 67 |
|
public static final String SIGNATURECLASS_PROP_SIGNATURE = "signature"; |
| 68 |
|
|
| 69 |
|
|
| 70 |
|
|
| 71 |
|
|
| 72 |
|
@Inject |
| 73 |
|
private Provider<XWikiContext> contextProvider; |
| 74 |
|
|
| 75 |
|
|
| 76 |
|
|
| 77 |
|
|
| 78 |
|
@Inject |
| 79 |
|
@Named("current") |
| 80 |
|
private DocumentReferenceResolver<EntityReference> resolver; |
| 81 |
|
|
| 82 |
|
|
| 83 |
|
|
| 84 |
|
|
| 85 |
|
@Inject |
| 86 |
|
@Named("Base64") |
| 87 |
|
private BinaryStringEncoder base64; |
| 88 |
|
|
| |
|
| 88.2% |
Uncovered Elements: 2 (17) |
Complexity: 4 |
Complexity Density: 0.27 |
|
| 89 |
2 |
@Override... |
| 90 |
|
public void store(EntityReference entity, byte[] signature) throws SignatureStoreException |
| 91 |
|
{ |
| 92 |
2 |
checkArguments(entity); |
| 93 |
|
|
| 94 |
|
|
| 95 |
2 |
DocumentReference docRef = this.resolver.resolve(entity.getParent()); |
| 96 |
2 |
XWikiContext context = this.contextProvider.get(); |
| 97 |
|
|
| 98 |
2 |
try { |
| 99 |
2 |
XWikiDocument document = context.getWiki().getDocument(docRef, context); |
| 100 |
|
|
| 101 |
2 |
DocumentReference signatureClass = new DocumentReference(SIGNATURECLASS, docRef.getWikiReference()); |
| 102 |
|
|
| 103 |
2 |
String entityReference = entity.getName(); |
| 104 |
|
|
| 105 |
2 |
BaseObject object = document.getXObject(signatureClass, SIGNATURECLASS_PROP_REFERENCE, entityReference); |
| 106 |
2 |
if (object == null) { |
| 107 |
1 |
object = document.newXObject(SIGNATURECLASS, context); |
| 108 |
1 |
object.setStringValue(SIGNATURECLASS_PROP_REFERENCE, entityReference); |
| 109 |
|
} |
| 110 |
|
|
| 111 |
2 |
object.setLargeStringValue(SIGNATURECLASS_PROP_SIGNATURE, this.base64.encode(signature, 64)); |
| 112 |
|
|
| 113 |
2 |
context.getWiki().saveDocument(document, context); |
| 114 |
|
} catch (XWikiException e) { |
| 115 |
0 |
throw new SignatureStoreException("Error while storing signature for entity [" + entity + "]", e); |
| 116 |
|
} catch (IOException e) { |
| 117 |
0 |
throw new SignatureStoreException("Error while encoding signature for entity [" + entity + "]", e); |
| 118 |
|
} |
| 119 |
|
} |
| 120 |
|
|
| |
|
| 80% |
Uncovered Elements: 4 (20) |
Complexity: 5 |
Complexity Density: 0.31 |
|
| 121 |
2 |
@Override... |
| 122 |
|
public byte[] retrieve(EntityReference entity) throws SignatureStoreException |
| 123 |
|
{ |
| 124 |
2 |
checkArguments(entity); |
| 125 |
|
|
| 126 |
2 |
DocumentReference docRef = this.resolver.resolve(entity); |
| 127 |
2 |
XWikiContext context = this.contextProvider.get(); |
| 128 |
|
|
| 129 |
2 |
try { |
| 130 |
2 |
XWikiDocument document = context.getWiki().getDocument(docRef, context); |
| 131 |
2 |
DocumentReference signatureClass = new DocumentReference(SIGNATURECLASS, docRef.getWikiReference()); |
| 132 |
|
|
| 133 |
2 |
String entityReference = entity.getName(); |
| 134 |
|
|
| 135 |
2 |
BaseObject object = document.getXObject(signatureClass, SIGNATURECLASS_PROP_REFERENCE, entityReference); |
| 136 |
2 |
if (object == null) { |
| 137 |
1 |
return null; |
| 138 |
|
} |
| 139 |
|
|
| 140 |
1 |
String signature = object.getLargeStringValue(SIGNATURECLASS_PROP_SIGNATURE); |
| 141 |
1 |
if (signature == null) { |
| 142 |
0 |
return null; |
| 143 |
|
} |
| 144 |
|
|
| 145 |
1 |
return this.base64.decode(signature); |
| 146 |
|
} catch (XWikiException e) { |
| 147 |
0 |
throw new SignatureStoreException("Error while retrieving signature for entity [" + entity + "]", e); |
| 148 |
|
} catch (IOException e) { |
| 149 |
0 |
throw new SignatureStoreException("Error while decoding signature for entity [" + entity + "]", e); |
| 150 |
|
} |
| 151 |
|
} |
| 152 |
|
|
| |
|
| 50% |
Uncovered Elements: 2 (4) |
Complexity: 4 |
Complexity Density: 2 |
|
| 153 |
4 |
private void checkArguments(EntityReference entity)... |
| 154 |
|
{ |
| 155 |
|
|
| 156 |
4 |
if (entity.getType() != EntityType.BLOCK || entity.getParent() == null |
| 157 |
|
|| entity.getParent().getType() != EntityType.DOCUMENT) { |
| 158 |
0 |
throw new IllegalArgumentException("Unsupported reference type [" + entity.getType() + "] or parent [" |
| 159 |
|
+ "]. Only blocks of documents are supported."); |
| 160 |
|
} |
| 161 |
|
} |
| 162 |
|
} |