/*
 * Copyright (c) 2012-2018 Red Hat, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Red Hat, Inc. - initial API and implementation
 */
package org.eclipse.che.wsagent.server;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import javax.validation.constraints.NotNull;
import org.eclipse.che.api.core.rest.DefaultHttpJsonRequestFactory;
import org.eclipse.che.api.core.rest.HttpJsonRequest;
import org.eclipse.che.api.core.rest.shared.dto.Link;

/**
 * Implementation of {@link org.eclipse.che.api.core.rest.HttpJsonRequestFactory} that add
 * ```machine.token``` as authorization header. Used to make request from ws-agent to ws-master.
 */
@Singleton
public class AgentHttpJsonRequestFactory extends DefaultHttpJsonRequestFactory {

  private final String machineToken;

  @Inject
  public AgentHttpJsonRequestFactory(@Named("machine.token") String machineToken) {
    this.machineToken = machineToken;
  }

  @Override
  public HttpJsonRequest fromUrl(@NotNull String url) {
    return super.fromUrl(url).setAuthorizationHeader(machineToken);
  }

  @Override
  public HttpJsonRequest fromLink(@NotNull Link link) {
    return super.fromLink(link).setAuthorizationHeader(machineToken);
  }
}

@@ -706,16 +706,119 @@
 to.Link;
+%0Aimport org.eclipse.che.commons.env.EnvironmentContext;%0Aimport org.eclipse.che.commons.subject.Subject;
 %0A%0A/**%0A *
@@ -1388,33 +1388,36 @@
 orizationHeader(
-m
+getM
 achineToken);%0A  
@@ -1407,24 +1407,26 @@
 MachineToken
+()
 );%0A  %7D%0A%0A  @O
@@ -1544,16 +1544,262 @@
 nHeader(
+getMachineToken());%0A  %7D%0A%0A  private String getMachineToken() %7B%0A    Subject subject = EnvironmentContext.getCurrent().getSubject();%0A    if (subject != null && subject.getToken() != null) %7B%0A      return subject.getToken();%0A    %7D else %7B%0A      return 
 machineT
@@ -1802,17 +1802,22 @@
 ineToken
-)
 ;
+%0A    %7D
 %0A  %7D%0A%7D%0A

Fix machine token auth by using own user machine token (#10249)