Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/version2.1.0' into version2.1.0
Browse files Browse the repository at this point in the history
# Conflicts:
#	back-end-projects/Explorer/config/application.properties
  • Loading branch information
zzsZhou committed May 7, 2020
2 parents 20b91e2 + 13f0406 commit b4d7746
Show file tree
Hide file tree
Showing 74 changed files with 2,861 additions and 95 deletions.
1 change: 1 addition & 0 deletions back-end-projects/Explorer/account.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"accounts":[],"createTime":"2020-03-26T10:46:52Z","defaultAccountAddress":"","defaultOntid":"did:ont:ARR7Rk4hpvnkAAWkyHGtrF2H3htHWrKFby","identities":[{"controls":[{"address":"ARR7Rk4hpvnkAAWkyHGtrF2H3htHWrKFby","algorithm":"ECDSA","enc-alg":"aes-256-gcm","hash":"sha256","id":"keys-1","key":"KDCGqt8JbwcN6wKQasQcUrDjjIUy152SfE1dIVvQjYGmuhzd74dnLnLhjgfJAkJY","parameters":{"curve":"P-256"},"publicKey":"034044d1ab03d42543e806c4242fe0ab42f0df530c7548f2078e00a79b087892bb","salt":"hf/9mY5kH9567GZQYnn4hw=="}],"isDefault":true,"label":"c7b82a36","lock":false,"ontid":"did:ont:ARR7Rk4hpvnkAAWkyHGtrF2H3htHWrKFby"},{"controls":[{"address":"Ae6XnePYWiBUALD2c2EdnuDAAWEsqf2JGr","algorithm":"ECDSA","enc-alg":"aes-256-gcm","hash":"sha256","id":"keys-1","key":"zgNPyJb3UtIA2jdHoS2I6RgnYPRc50cC89gIN9frbnZMtp+pMSm9iR3Oxd9/kIBQ","parameters":{"curve":"P-256"},"publicKey":"0376104dd7bba89580540f9e923a32ad9470af8759774c578936d96004fc492218","salt":"oNyhAmy2RLtv64wmnAlK6g=="}],"isDefault":false,"label":"06c59f9d","lock":false,"ontid":"did:ont:Ae6XnePYWiBUALD2c2EdnuDAAWEsqf2JGr"}],"name":"com.github.ontio","scrypt":{"dkLen":64,"n":16384,"p":8,"r":8},"version":"1.0"}
14 changes: 14 additions & 0 deletions back-end-projects/Explorer/config/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,20 @@ reqlimit.expire.millisecond=1000

swagger.enable=true


##user login
login.callbackUrl=
login.qrcode.url=

identity.ontid =
identity.password =
identity.salt =

jwt.accessTokenExpireTime=120
jwt.encryptJWTKey=

oneUser.address.count.limit=5

coinmarketcap.api.key=
coinmarketcap.api.host=https://pro-api.coinmarketcap.com/
coinmarketcap.refresh.interval=15
7 changes: 7 additions & 0 deletions back-end-projects/Explorer/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<java.version>1.8</java.version>
<springfox.version>2.9.2</springfox.version>
<retrofit2.version>2.3.0</retrofit2.version>
<jwt.version>3.4.1</jwt.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -185,6 +186,12 @@
<version>${retrofit2.version}</version>
</dependency>

<dependency>
<groupId>com.auth0</groupId>
<artifactId>java-jwt</artifactId>
<version>${jwt.version}</version>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package com.github.ontio;

import com.github.ontio.util.ConstantParam;
import org.springframework.stereotype.Component;

import javax.servlet.*;
Expand All @@ -29,26 +30,26 @@
@Component
public class SimpleCORSFilter implements Filter {

public void doFilter(ServletRequest req, ServletResponse res,
public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse) res;
HttpServletRequest request = (HttpServletRequest) req;
String origin = request.getHeader("Origin");
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Credentials", "true");
response.setHeader("Access-Control-Allow-Methods",
"POST, PUT, GET, OPTIONS, DELETE");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader(
"Access-Control-Allow-Headers",
"Origin, No-Cache, X-Requested-With, If-Modified-Since, Pragma, Last-Modified, Cache-Control, Expires, Content-Type, X-E4M-With");
chain.doFilter(req, res);
}

public void init(FilterConfig filterConfig) {
}

public void destroy() {
}
HttpServletResponse response = (HttpServletResponse) res;
HttpServletRequest request = (HttpServletRequest) req;
String origin = request.getHeader("Origin");
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Credentials", "true");
response.setHeader("Access-Control-Allow-Methods",
"POST, PUT, GET, OPTIONS, DELETE");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader(
"Access-Control-Allow-Headers", request.getHeader("Access-Control-Request-Headers"));
response.setHeader("Access-Control-Expose-Headers", ConstantParam.HTTPHEADER_TOKEN);
chain.doFilter(req, res);
}

public void init(FilterConfig filterConfig) {
}

public void destroy() {
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,23 @@ public String getContractHash(String token) {
}
}

@Value("${login.callbackUrl:https://explorer.ont.io/v2/users/login}")
public String loginCallbackUrl;

@Value("${identity.ontid}")
public String IDENTITY_ONTID;

@Value("${identity.password}")
public String IDENTITY_PASSWORD;

@Value("${identity.salt}")
public String IDENTITY_SALT;

@Value("${login.qrcode.url:https://explorer.ont.io/v2/users/login_qrcode/}")
public String loginQrCodeUrl;


@Value("${oneUser.address.count.limit}")
public Integer oneUserAddressCountLimit;

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
Expand Down Expand Up @@ -72,9 +68,9 @@ public ResponseBean queryTokenDetail(@PathVariable("token_type") @Pattern(regexp
@GetMapping(value = "/oep8/{contract_hash}/{token_name}/transactions")
public ResponseBean queryOep8TxsByPage(@PathVariable("contract_hash") @Length(min = 40, max = 40, message = "Incorrect " +
"contract hash") String contractHash,
@PathVariable("token_name") String tokenName,
@RequestParam("page_size") @Min(1) @Max(20) Integer pageSize,
@RequestParam("page_number") @Min(1) Integer pageNumber) {
@PathVariable("token_name") String tokenName,
@RequestParam("page_size") @Min(1) @Max(20) Integer pageSize,
@RequestParam("page_number") @Min(1) Integer pageNumber) {

log.info("###{}.{} begin...contract_hash:{},token_name:{}", CLASS_NAME, Helper.currentMethod(), contractHash, tokenName);

Expand Down Expand Up @@ -120,4 +116,14 @@ public ResponseBean queryPrice(
return tokenService.queryPrice(token, fiat);
}


@ApiOperation(value = "Get oep logos")
@GetMapping(value = "/logos")
public ResponseBean queryOepLogos(@RequestParam("contract_hash") @Length(min = 40, max = 40, message = "Incorrect contract hash") String contractHash,
@RequestParam("page_size") @Min(1) @Max(50) int pageSize,
@RequestParam("page_number") @Min(1) int pageNumber) {
log.info("###{}.{} begin...", CLASS_NAME, Helper.currentMethod());
return tokenService.queryOepLogos(contractHash, pageSize, pageNumber);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
package com.github.ontio.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.ontio.exception.ExplorerException;
import com.github.ontio.model.common.ResponseBean;
import com.github.ontio.model.dao.User;
import com.github.ontio.model.dao.UserAddress;
import com.github.ontio.model.dto.login.CallBackDto;
import com.github.ontio.model.dto.login.CallBackResponse;
import com.github.ontio.model.dto.login.QrCodeDto;
import com.github.ontio.service.IUserService;
import com.github.ontio.util.ConstantParam;
import com.github.ontio.util.ErrorInfo;
import com.github.ontio.util.Helper;
import com.github.ontio.util.JwtUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.Pattern;
import java.util.List;

/**
* @author zhouq
* @version 1.0
* @date 2020/3/24
*/
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/v2/users/")
@Validated
public class UserController {

private final String CLASS_NAME = this.getClass().getSimpleName();

private final IUserService userService;


@ApiOperation(value = "Web Query login qrcode")
@GetMapping(value = "/web_login_qrcode")
public ResponseBean queryWebQrCode() {
ResponseBean rs = userService.queryWebQrCode();
return rs;
}

@ApiOperation(value = "ONTO APP Query login qrcode")
@GetMapping(value = "/login_qrcode/{qrcode_id}")
public QrCodeDto queryQrCode(@PathVariable("qrcode_id") String qrcodeId) {
QrCodeDto rs = userService.queryQrCode(qrcodeId);
return rs;
}


@ApiOperation(value = "Query login user info")
@GetMapping(value = "/login_user_info")
public ResponseBean queryLoginUserInfo(@RequestParam("code") String code) {
ResponseBean rs = userService.queryLoginUserInfo(code);
return rs;
}


@ApiOperation(value = "ONTO User login")
@PostMapping(value = "/login")
public CallBackResponse userLogin(@RequestBody JSONObject jsonObject) {
log.info("###{}.{} begin...param:{}", CLASS_NAME, Helper.currentMethod(), jsonObject);
CallBackDto callBackDto = new CallBackDto();
callBackDto.setSigner(jsonObject.getString("signer"));
callBackDto.setSignedTx(jsonObject.getString("signedTx"));
CallBackDto.CallbackExtraData callbackExtraData = CallBackDto.CallbackExtraData.builder()
.id(jsonObject.getJSONObject("extraData").getString("id"))
.build();
callBackDto.setExtraData(callbackExtraData);
CallBackResponse rs = userService.login(callBackDto);
return rs;
}


@ApiImplicitParams({@ApiImplicitParam(paramType = "header", dataType = "String", name = "ONT_EXP_TOKEN", value = "login token", required = true)})
@ApiOperation(value = "Query user addresses")
@GetMapping(value = "/addresses")
public ResponseBean queryUserAddresses(@RequestParam("ont_id") @Pattern(regexp = "did:ont:[A-Za-z0-9]{34}", message = "Incorrect ONT ID format") String ontId) {
log.info("###{}.{} begin...ontId:{}", CLASS_NAME, Helper.currentMethod(), ontId);
checkToken(ontId);
ResponseBean rs = userService.queryUserAddresses(ontId);
refreshToken(ontId);
return rs;
}


@ApiImplicitParams({@ApiImplicitParam(paramType = "header", dataType = "String", name = "ONT_EXP_TOKEN", value = "login token", required = true)})
@ApiOperation(value = "Add or Update user addresses")
@PostMapping(value = "/addresses")
public ResponseBean addOrUpdateUserAddresses(@RequestParam("ont_id") @Pattern(regexp = "did:ont:[A-Za-z0-9]{34}", message = "Incorrect ONT ID format") String ontId,
@RequestBody @Valid List<UserAddress> userAddresses) {
log.info("###{}.{} begin...ontId:{}", CLASS_NAME, Helper.currentMethod(), ontId);
checkToken(ontId);
ResponseBean rs = userService.addOrUpdateUserAddresses(userAddresses, ontId);
refreshToken(ontId);
return rs;
}


@ApiImplicitParams({@ApiImplicitParam(paramType = "header", dataType = "String", name = "ONT_EXP_TOKEN", value = "login token", required = true)})
@ApiOperation(value = "Delete user address")
@DeleteMapping(value = "/addresses")
public ResponseBean delUserAddress(@RequestParam("ont_id") @Pattern(regexp = "did:ont:[A-Za-z0-9]{34}", message = "Incorrect ONT ID format") String ontId,
@RequestBody JSONObject jsonObject) {
log.info("###{}.{} begin...ontId:{}", CLASS_NAME, Helper.currentMethod(), ontId);
checkToken(ontId);
ResponseBean rs = userService.delUserAddress(jsonObject.getString("address"), ontId);
refreshToken(ontId);
return rs;
}


@ApiImplicitParams({@ApiImplicitParam(paramType = "header", dataType = "String", name = "ONT_EXP_TOKEN", value = "login token", required = true)})
@ApiOperation(value = "Update user information")
@PostMapping
public ResponseBean updateUser(@RequestParam("ont_id") @Pattern(regexp = "did:ont:[A-Za-z0-9]{34}", message = "Incorrect ONT ID format") String ontId,
@RequestBody @Validated User user) {
log.info("###{}.{} begin...ontId:{}", CLASS_NAME, Helper.currentMethod(), ontId);
checkToken(ontId);
user.setOntId(ontId);
ResponseBean rs = userService.updateUser(user);
refreshToken(ontId);
return rs;
}


private void checkToken(String ontId) {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
String token = request.getHeader(ConstantParam.HTTPHEADER_TOKEN);
if (Helper.isEmptyOrNull(token)) {
throw new ExplorerException(ErrorInfo.TOKEN_EMPTY);
}
if (!JwtUtil.verifyToken(token)) {
throw new ExplorerException(ErrorInfo.TOKEN_EXPIRED);
} else if (!JwtUtil.getClaim(token, ConstantParam.JWT_LOGINID).asString().equals(ontId)) {
throw new ExplorerException(ErrorInfo.TOKEN_UNMATCH);
}
}

private void refreshToken(String ontId) {
String newToken = JwtUtil.signToken(ontId);
HttpServletResponse resp = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
resp.setHeader(ConstantParam.HTTPHEADER_TOKEN, newToken);
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.ontio.exception;

import com.github.ontio.util.ErrorInfo;
import lombok.Data;
import lombok.EqualsAndHashCode;

Expand All @@ -19,6 +20,18 @@ public ExplorerException(Integer code, String msg, Object result){
this.result = result;
}

public ExplorerException(ErrorInfo errorInfo, Object result){
this.code = errorInfo.code();
this.msg = errorInfo.desc();
this.result = result;
}

public ExplorerException(ErrorInfo errorInfo){
this.code = errorInfo.code();
this.msg = errorInfo.desc();
this.result = false;
}

public ExplorerException(){super();}

public ExplorerException(String msg){super(msg);}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.github.ontio.mapper;

import com.github.ontio.model.dao.AddressBlacklist;
import tk.mybatis.mapper.common.Mapper;

public interface AddressBlacklistMapper extends Mapper<AddressBlacklist> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.github.ontio.mapper;

import com.github.ontio.model.dao.OepLogo;
import tk.mybatis.mapper.common.Mapper;

public interface OepLogoMapper extends Mapper<OepLogo> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.github.ontio.mapper;

import com.github.ontio.model.dao.UserAddress;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;

public interface UserAddressMapper extends Mapper<UserAddress> {

int saveUserAddress(List<UserAddress> userAddresses);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.github.ontio.mapper;

import com.github.ontio.model.dao.User;
import tk.mybatis.mapper.common.Mapper;

public interface UserMapper extends Mapper<User> {

int saveUser(User user);

}
Loading

0 comments on commit b4d7746

Please sign in to comment.