Example usage for org.apache.wicket.util.crypt Base64 encodeBase64

List of usage examples for org.apache.wicket.util.crypt Base64 encodeBase64

Introduction

In this page you can find the example usage for org.apache.wicket.util.crypt Base64 encodeBase64.

Prototype

public static byte[] encodeBase64(final byte[] binaryData) 

Source Link

Document

Encodes binary data using the base64 algorithm but does not chunk the output.

Usage

From source file:com.doculibre.constellio.entities.ConstellioUser.java

License:Open Source License

/**
 * Generates a hash for password using salt from IAuthSettings.getSalt()
 * and returns the hash encoded as a Base64 String.
 * /*from www  .jav  a  2  s.  co  m*/
 * @see IAuthSettings#getSalt()
 * @param password
 *            to encode
 * @return base64 encoded SHA hash, 28 characters
 */
public static String getHash(String password) {
    MessageDigest md = getMessageDigest();
    md.update(getSalt());
    byte[] hash = md.digest(password.getBytes());
    // using a Base64 string for the hash because putting a
    // byte[] into a blob isn't working consistently.
    return new String(Base64.encodeBase64(hash));
}

From source file:net.databinder.auth.components.RSAPasswordTextField.java

License:Open Source License

protected void init(Form form) {
    setOutputMarkupId(true);/*from w  ww .  j a v a  2  s  . com*/

    add(new AttributeAppender("style", new Model<String>("visibility:hidden"), ";"));

    form.add(new AttributeAppender("onsubmit", new AbstractReadOnlyModel() {
        public Object getObject() {
            StringBuilder eventBuf = new StringBuilder();
            eventBuf.append("if (").append(getElementValue()).append(" != null && ").append(getElementValue())
                    .append(" != '') ").append(getElementValue()).append(" = encryptedString(key, ")
                    .append(getChallengeVar()).append("+ '|' + ").append(getElementValue()).append(");");

            return eventBuf.toString();
        }
    }, ""));

    challenge = new String(
            Base64.encodeBase64(BigInteger.valueOf(new SecureRandom().nextLong()).toByteArray()));
}

From source file:net.databinder.auth.hib.AuthDataApplication.java

License:Open Source License

/**
 * Get the restricted token for a user, using IP addresses as location parameter. This implementation
 * combines the "X-Forwarded-For" header with the remote address value so that unique
 * values result with and without proxying. (The forwarded header is not trusted on its own
 * because it can be most easily spoofed.)
 * @param user source of token//w w w .  ja v  a  2s.  c o  m
 * @return restricted token
 */
public String getToken(DataUser user) {

    ServletWebRequest request = ((ServletWebRequest) RequestCycle.get().getRequest());
    HttpServletRequest req = request.getContainerRequest();
    String fwd = req.getHeader("X-Forwarded-For");
    if (fwd == null)
        fwd = "nil";
    MessageDigest digest = getDigest();
    user.getPassword().update(digest);
    digest.update((fwd + "-" + req.getRemoteAddr()).getBytes());
    byte[] hash = digest.digest(user.getUsername().getBytes());
    return new String(Base64.encodeBase64(hash));
}

From source file:org.apache.syncope.client.console.commons.PreferenceManager.java

License:Apache License

public void set(final Request request, final Response response, final Map<String, List<String>> prefs) {
    Map<String, String> current = new HashMap<>();

    String prefString = cookieUtils.load(PREFMAN_KEY);
    if (prefString != null) {
        current.putAll(getPrefs(new String(Base64.decodeBase64(prefString.getBytes()))));
    }/*from ww w  .j av  a  2 s  .  co  m*/

    // after retrieved previous setting in order to overwrite the key ...
    for (Map.Entry<String, List<String>> entry : prefs.entrySet()) {
        current.put(entry.getKey(), StringUtils.collectionToDelimitedString(entry.getValue(), ";"));
    }

    try {
        cookieUtils.save(PREFMAN_KEY, new String(Base64.encodeBase64(setPrefs(current).getBytes())));
    } catch (IOException e) {
        LOG.error("Could not save {} info: {}", getClass().getSimpleName(), current, e);
    }
}

From source file:org.apache.syncope.client.console.commons.PreferenceManager.java

License:Apache License

public void set(final Request request, final Response response, final String key, final String value) {
    String prefString = cookieUtils.load(PREFMAN_KEY);

    final Map<String, String> current = new HashMap<>();
    if (prefString != null) {
        current.putAll(getPrefs(new String(Base64.decodeBase64(prefString.getBytes()))));
    }//from   ww  w .  j av  a  2 s  .c  o m

    // after retrieved previous setting in order to overwrite the key ...
    current.put(key, value);

    try {
        cookieUtils.save(PREFMAN_KEY, new String(Base64.encodeBase64(setPrefs(current).getBytes())));
    } catch (IOException e) {
        LOG.error("Could not save {} info: {}", getClass().getSimpleName(), current, e);
    }
}

From source file:org.apache.syncope.client.console.PreferenceManager.java

License:Apache License

public void set(final Request request, final Response response, final Map<String, List<String>> prefs) {
    Map<String, String> current = new HashMap<>();

    String prefString = COOKIE_UTILS.load(COOKIE_NAME);
    if (prefString != null) {
        current.putAll(getPrefs(new String(Base64.decodeBase64(prefString.getBytes()))));
    }//from   ww w .j  a  v a2  s . c o  m

    // after retrieved previous setting in order to overwrite the key ...
    for (Map.Entry<String, List<String>> entry : prefs.entrySet()) {
        current.put(entry.getKey(), StringUtils.join(entry.getValue(), ";"));
    }

    try {
        COOKIE_UTILS.save(COOKIE_NAME, new String(Base64.encodeBase64(setPrefs(current).getBytes())));
    } catch (IOException e) {
        LOG.error("Could not save {} info: {}", getClass().getSimpleName(), current, e);
    }
}

From source file:org.apache.syncope.client.console.PreferenceManager.java

License:Apache License

public void set(final Request request, final Response response, final String key, final String value) {
    String prefString = COOKIE_UTILS.load(COOKIE_NAME);

    final Map<String, String> current = new HashMap<>();
    if (prefString != null) {
        current.putAll(getPrefs(new String(Base64.decodeBase64(prefString.getBytes()))));
    }//from   w  ww  .  ja  v  a2  s.  co  m

    // after retrieved previous setting in order to overwrite the key ...
    current.put(key, value);

    try {
        COOKIE_UTILS.save(COOKIE_NAME, new String(Base64.encodeBase64(setPrefs(current).getBytes())));
    } catch (IOException e) {
        LOG.error("Could not save {} info: {}", getClass().getSimpleName(), current, e);
    }
}

From source file:org.apache.syncope.client.console.wicket.markup.html.form.BinaryFieldPanel.java

License:Apache License

public BinaryFieldPanel(final String id, final String name, final IModel<String> model, final String mimeType) {
    super(id, name, model);
    this.mimeType = mimeType;

    previewer = previewUtils.getPreviewer(mimeType);

    uploadForm = new StatelessForm<>("uploadForm");
    uploadForm.setMultiPart(true);/*from   ww  w.j  a v a 2  s .  c o  m*/
    uploadForm.setMaxSize(Bytes.megabytes(4));
    add(uploadForm);

    container = new WebMarkupContainer("previewContainer") {

        private static final long serialVersionUID = 2628490926588791229L;

        @Override
        public void renderHead(final IHeaderResponse response) {
            if (previewer == null) {
                FileinputJsReference.INSTANCE.renderHead(response);
                final JQuery fileinputJS = $(fileUpload).chain(new IFunction() {

                    private static final long serialVersionUID = -2285418135375523652L;

                    @Override
                    public String build() {
                        return "fileinput({" + "'showRemove':false, " + "'showUpload':false, "
                                + "'previewFileType':'any'})";
                    }
                });
                response.render(OnDomReadyHeaderItem.forScript(fileinputJS.get()));
            }
        }
    };
    container.setOutputMarkupId(true);

    emptyFragment = new Fragment("panelPreview", "emptyFragment", container);
    emptyFragment.setOutputMarkupId(true);
    container.add(emptyFragment);
    uploadForm.add(container);

    field = new TextField<>("textField", model);
    add(field.setLabel(new Model<>(name)).setOutputMarkupId(true));

    uploadForm.add(
            new Label("preview", StringUtils.isBlank(mimeType) ? StringUtils.EMPTY : "(" + mimeType + ")"));

    downloadLink = new AjaxLink<Void>("downloadLink") {

        private static final long serialVersionUID = -4331619903296515985L;

        @Override
        public void onClick(final AjaxRequestTarget target) {
            try {
                HttpResourceStream stream = new HttpResourceStream(buildResponse());

                ResourceStreamRequestHandler rsrh = new ResourceStreamRequestHandler(stream);
                rsrh.setFileName(stream.getFilename() == null ? name : stream.getFilename());
                rsrh.setContentDisposition(ContentDisposition.ATTACHMENT);

                getRequestCycle().scheduleRequestHandlerAfterCurrent(rsrh);
            } catch (Exception e) {
                SyncopeConsoleSession.get()
                        .error(StringUtils.isBlank(e.getMessage()) ? e.getClass().getName() : e.getMessage());
            }
        }
    };
    downloadLink.setOutputMarkupId(true);
    uploadForm.add(downloadLink);

    FileInputConfig config = new FileInputConfig();
    config.showUpload(false);
    config.showRemove(false);
    config.showPreview(false);

    fileUpload = new BootstrapFileInputField("fileUpload", new ListModel<>(new ArrayList<FileUpload>()),
            config);
    fileUpload.setOutputMarkupId(true);

    fileUpload.add(new AjaxFormSubmitBehavior(Constants.ON_CHANGE) {

        private static final long serialVersionUID = -1107858522700306810L;

        @Override
        protected void onSubmit(final AjaxRequestTarget target) {
            final FileUpload uploadedFile = fileUpload.getFileUpload();
            if (uploadedFile != null) {
                final byte[] uploadedBytes = uploadedFile.getBytes();
                final String uploaded = new String(Base64.encodeBase64(uploadedBytes),
                        SyncopeConstants.DEFAULT_CHARSET);
                field.setModelObject(uploaded);
                target.add(field);

                if (previewer == null) {
                    container.addOrReplace(emptyFragment);
                } else {
                    final Component panelPreview = previewer.preview(uploadedBytes);
                    changePreviewer(panelPreview);
                    fileUpload.setModelObject(null);
                    uploadForm.addOrReplace(fileUpload);
                }

                downloadLink.setEnabled(StringUtils.isNotBlank(uploaded));

                target.add(container);
            }
        }
    });
    uploadForm.add(fileUpload);

    IndicatingAjaxLink<Void> resetLink = new IndicatingAjaxLink<Void>("resetLink") {

        private static final long serialVersionUID = -7978723352517770644L;

        @Override
        public void onClick(final AjaxRequestTarget target) {
            field.setModelObject(null);
            target.add(field);
            downloadLink.setEnabled(false);
            container.addOrReplace(emptyFragment);
            uploadForm.addOrReplace(container);
            target.add(uploadForm);
        }
    };
    uploadForm.add(resetLink);
}

From source file:org.apache.syncope.console.commons.PreferenceManager.java

License:Apache License

public void set(final Request request, final Response response, final Map<String, List<String>> prefs) {
    String prefString = cookieUtils.load(PREFMAN_KEY);

    final Map<String, String> current = new HashMap<String, String>();
    if (prefString != null) {
        current.putAll(getPrefs(new String(Base64.decodeBase64(prefString.getBytes()))));
    }/*  w  ww.j ava  2 s.  c  o  m*/

    // after retrieved previous setting in order to overwrite the key ...
    for (Entry<String, List<String>> entry : prefs.entrySet()) {
        current.put(entry.getKey(), StringUtils.collectionToDelimitedString(entry.getValue(), ";"));
    }

    try {
        cookieUtils.save(PREFMAN_KEY, new String(Base64.encodeBase64(setPrefs(current).getBytes())));
    } catch (IOException e) {
        LOG.error("Could not save {} info: {}", getClass().getSimpleName(), current, e);
    }
}

From source file:org.apache.syncope.console.commons.PreferenceManager.java

License:Apache License

public void set(final Request request, final Response response, final String key, final String value) {
    String prefString = cookieUtils.load(PREFMAN_KEY);

    final Map<String, String> current = new HashMap<String, String>();
    if (prefString != null) {
        current.putAll(getPrefs(new String(Base64.decodeBase64(prefString.getBytes()))));
    }//from  w  ww. ja  v a2  s  .c  o m

    // after retrieved previous setting in order to overwrite the key ...
    current.put(key, value);

    try {
        cookieUtils.save(PREFMAN_KEY, new String(Base64.encodeBase64(setPrefs(current).getBytes())));
    } catch (IOException e) {
        LOG.error("Could not save {} info: {}", getClass().getSimpleName(), current, e);
    }
}